Restoring real user IP addresses from Cloudflare in Apache

Upon provisioning a fresh VM for Grand Visual‘s new website, I needed to get Cloudflare to pass on visitors’ real IP addresses on to Apache. By default I was seeing only Cloudflare’s IP addresses in the visitor log.

Having the real IP address of your visitors has many benefits, mainly from a security point-of-view. Once you can see real IP addresses then you can have WordPress automatically block the IP address of spammers/potential crackers.

For some reason, when I followed the tutorial on Cloudflare’s site I was unable to get Apache’s access log to report the correct IP. However after doing some digging around online, I found that Cloudflare also offer a .deb package. This method doesn’t seem to be greatly documented online which seems kind of strange. After running through this route Apache was finally behaving itself.

Grab the package

wget https://www.cloudflare.com/static/misc/mod_cloudflare/ubuntu/mod_cloudflare-xenial-amd64.latest.deb

Install the package

sudo dpkg -i mod_cloudflare-xenial-amd64.latest.deb

Restart Apache

sudo service apache2 restart

Now when you check Apache’s visitor log you should see real IP addresses.

Leave a Reply

Your email address will not be published. Required fields are marked *