I have been using cloudflare for Nam Huy Linux for while and I have to say cloudflare helps my website to reduce request and speed up my website by distribute static files to visitors globally with CDN (content delivery network). They have 24 locations around the world right now from Asian, North America and Europe. A bonus plus with cloudflare is they can protect you from DDOS attack and other type of attacks.
Cloudflare will stay in front of you web server, it will block all the threats that may slow down your web server or even make it crash. Cloudflare also offer some other nice tweaks to speed up your website like javascript, css, html optimization before caching them and deliver to visitors.
Using CDN or cloudflare brings many advantages for your websites, but it has a bit of problem when it comes to showing real IP from your website’s visitors. I’m running wordpress and phpbb for my namhuy.net. I will show you how to show real/original IP for wordpress and phpbb with cloudflare in this article.
Firstly, if you have access to your web server, VPS (virtual private server) or dedicated server, you should install mod_cloudflare module for apache. Please go to https://www.cloudflare.com/resources-downloads to get the latest mod_cloudflare for your linux distribution.
For RedHat / CentOS / CloudLinux:
# yum install glibc.i686 # rpm -i mod_cloudflare-elX.latest.rpm
For Debian / Ubuntu:
# dpkg -i mod_cloudflare-XXXXX.latest.deb
Restart apache after you install mod_cloudflare module
# service httpd restart
To show original/real IP for wordpress with cloudflare
You can download cloudflare wordpress plugin which developed by cloudflare. After install, and active cloudflare plugin for wordpress, you should sign up an free account from cloudflare to get CloudFlare API Key for the plugin. To setup the CloudFlare API Key in wordpress, login to your wp-admin, go to Plugins/Cloudflare. You will be able to set your CloudFlare API Key and CloudFlare API Email. That is pretty much you have to do to show real or original IP for wordpress with cloudflare.
In the even of you use varnish with apache like me, I just recently install varnish for Nam Huy Linux. Varnish will always gives apache the localhost address which is 127.0.0.1 since I set varnish in front of apache. To show real/original IP for wordpress with varnish and cloudflare, you will have to modify the wordpress cloudflare plugin you just installed. In your wp-admin, go to Plugins/Editor.
In “Select plugin to edit:” box, please choose “cloudflare” and click “select”. Look for
if (strpos($_SERVER["REMOTE_ADDR"], ":") === FALSE) { $cf_ip_ranges = array("199.27.128.0/21","173.245.48.0/20","103.21.244.0/22","103.22.200.0/22","103.31.4.0/22","141.101.64.0/18","108.162.192.0/18","190.93.240.0/20","188.114.96.0/20","197.234.240.0/22","198.41.128.0/17","162.158.0.0/15");
Add this to the end
"127.0.0.1/32"
You should have something like this with 127.0.0.1 at the end of that code.
if (strpos($_SERVER["REMOTE_ADDR"], ":") === FALSE) { $cf_ip_ranges = array("199.27.128.0/21","173.245.48.0/20","103.21.244.0/22","103.22.200.0/22","103.31.4.0/22","141.101.64.0/18","108.162.192.0/18","190.93.240.0/20","188.114.96.0/20","197.234.240.0/22","198.41.128.0/17","162.158.0.0/15","127.0.0.1/32");
After you done, update the cloudflare file in wordpress by click “update” button at the end.
To show original/real IP for phpbb with cloudflare
Install mod_cloudflare module as I wrote above. In phpbb there isn’t an easy “Editor” like wordpress, In Cpanel Or Plesk, I believe they have some sort of file editor built in. What you have to do is modify includes/session.php file in phpbb directory. You can either use file editor from cpanel or plesk, or edit then download/upload that file via FTP.
The session.php should be located in phpbb/includes/session.php, look/search for
$this->ip = (!empty($_SERVER['REMOTE_ADDR'])) ? (string) $_SERVER['REMOTE_ADDR'] : '';
What you need to do is to replace REMOTE_ADDR TO HTTP_CF_CONNECTING_IP. You should have something like this after you change/replace:
$this->ip = (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) ? (string) $_SERVER['HTTP_CF_CONNECTING_IP'] : '';
Save includes/session.php file and you are done!
Note: here is the IPv4 and IPv6 list that cloudflare use
https://www.cloudflare.com/ips