Laravel Cloudflare logging real visitor IP address Server Solution

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:

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.

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:

# yum install libtool httpd-devel
# wget https://raw.githubusercontent.com/cloudflare/mod_cloudflare/master/mod_cloudflare.c
# 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

Previous Stories You will Love:

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

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