Laravel Cloudflare logging real visitor IP address Server Solution

Feb 21, 2019 · 3 min read
Image for post
Image for post


When using Cloudflare and Laravel we are unable to use request()->ip() in Laravel because of Cloudflare returns it's own IP in $_SERVER['REMOTE_ADDR'].


By default, Cloudflare acts as a reverse proxy (read more here) As such, all connections to your origin web server come from Cloudflare IP addresses, So there are some issues:

1- If your web application is using the originating IP of the visitor as part of its logic, it will now use a Cloudflare IP address instead

2- If you use the content of your access logs, they now contain a Cloudflare IP address as the $remote_addr

Solution 1: Code Manipulation

However, Cloudflare follows industry standards and includes the visitor’s IP address in the X-Forwarded-For header. It also add a CF-Connecting-IP header that may be used as well. You can use these headers to either restore the originating IP of your visitor for your web application or to be include it in your logs.

read more here and here

Solution 2: Server Solution

CloudFlare has release mod_cloudflare for Apache, which logs & displays the actual visitor IP address rather than the CloudFlare IP address. So the solution is to install mod_cloudflare for Apache httpd as follow:

Manual Installation: RedHat / CentOS / CloudLinux

mod_cloudflare has a few software dependencies that need to be installed first:

# yum install libtool httpd-devel

Next, you should download the mod_cloudflare source to your server:

# wget

Finally, install the module. Depending on your system, the command to run might be apxs or apxs2. So, run one of the below two commands. If you get a ‘Command not found’ when running one, try the other:

# apxs -a -i -c mod_cloudflare.c# apxs2 -a -i -c mod_cloudflare.c

Other platforms:

For other platforms refer to the original document this Link and this one

Thank you for reading! If you enjoyed this article:

Clap it ! Share it! Follow Me in Medium!

Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.

Have a very wonderful day!

Previous Stories You will Love:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store