{"id":11033,"date":"2021-12-07T16:06:14","date_gmt":"2021-12-07T21:06:14","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=11033"},"modified":"2021-12-07T16:44:16","modified_gmt":"2021-12-07T21:44:16","slug":"real-client-ip-address-cloudflare-apache-php","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/real-client-ip-address-cloudflare-apache-php\/","title":{"rendered":"Get the Real Client IP Address With Cloudflare CF-Connecting-IP in Apache | PHP"},"content":{"rendered":"\n
Cloudflare is a website security service from a company of the same name that provides content delivery network and DDoS mitigation services. For several good reasons, you want to know the Real Client IP Address of your visitors.<\/p>\n\n\n\n
With Cloudflare like any proxy, the webserver will not be able to tell what the visitor’s IP address is. This can open up several security issues because your Apache<\/a> access and error logs will not show the correct IP but rather those of the Cloudflare infrastructure.<\/p>\n\n\n\n It also becomes difficult to define IP restrictions in your web server configuration, .htaccess, or even in PHP<\/a> scripts.<\/p>\n\n\n\n To solve this problem, Cloudflare sends the real client IP as CF-Connecting-IP in the HTTP header. CF-Connecting-IP provides the real client (visitor) IP address to the origin web server. cf-connecting-ip contains a special Cloudflare IP 2a06:98c0:3600:0:0:0:0:103 when the request originates from a Cloudflare Workers subrequest instead of the visitor’s true IP.<\/p>\n\n\n\n This works similar to the x-forwarded-for header which is used by proxy servers to tell the origin of any HTTP servers involved in relaying the request between the user and the origin.<\/p>\n\n\n\n There are some differences between CF-Connecting-IP and x-forwarded-for headers. With x-forwarded-for, you may see one or multiple sets of IP addresses in this header while CF-Connecting-IP will always contain one IP.<\/p>\n\n\n\n If Cloudflare does not receive the x-forwarded-for header from the client it will pass the same value as CF-Connecting-IP to the origin web server<\/a>.<\/p>\n\n\n\n Cloudflare recommends your logs or applications look at CF-Connecting-IP or True-Client-IP instead of X-Forwarded-For since CF-Connecting-IP and True-Client-IP have a consistent format containing only one IP.<\/p>\n\n\n\n We can retrieve the value of CF-Connecting-IP on the origin web server by enabling Apache’s mod mod_remoteip.<\/p>\n\n\n\n First, you need to enable remoteip <\/em>on your server. In Ubuntu\/Debian as well as CentOS\/RHEL based systems run the following commands:<\/p>\n\n\n\n Then restart Apache:<\/p>\n\n\n\n Edit Apache Configs to Define Trusted Proxies<\/p>\n\n\n\n To pass the real client IP address from Cloudflare to Apache, we need to define the RemoteIPHeader directive as CF-Connecting-IP in the remoteip configuration file remoteip.conf<\/strong>.<\/p>\n\n\n\n Create the remoteip.conf<\/strong> configuration file by running this command in Ubuntu<\/a>\/Debian<\/a> Linux<\/a> systems.<\/p>\n\n\n\nReal Client IP Address Using CF-Connecting-IP<\/h2>\n\n\n\n
How to Handle CF-Connecting-IP on the Origin Server<\/h2>\n\n\n\n
$ sudo a2enmod remoteip<\/code><\/pre>\n\n\n\n
$ sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n
$ sudo nano \/etc\/apache2\/conf-enabled\/remoteip.conf<\/code><\/pre>\n\n\n\n