Laravel Cloudflare logging real visitor IP address Server Solution

Panjeh
Panjeh
Feb 21 · 3 min read

Issue:

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'].

Reason:

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:

Panjeh

Written by

Panjeh

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