When you use CloudFlare for your websites, you will see only CloudFlare’s IP addresses appearing in the logs. CloudFlare acts as a reverse proxy and includes the originating IP address in the X-Forwarded-For header
. Therefore it is possible to add the visitor’s real IP again to your logs.
For nginx it is necessary to have http-real-ip
installed. On Ubuntu, this module is activated by default. So we immediately can get started.
Add the following lines to /etc/nginx/nginx.conf
:
Create a new file /etc/nginx/cloudflare
and add these lines:
This is the list of IP addresses currently used by CloudFlare.
Now you can reload nginx and the real IP’s will be showing again in the logs.
If you get an error like this one:
$ service nginx reload
Reloading nginx configuration: nginx: [emerg] "set_real_ip_from" supports IPv4 only in /etc/nginx/nginx.conf:44
nginx: configuration file /etc/nginx/nginx.conf test failed
it just means you don’t support IPv6. Remove the lines with IPv6 addresses from the CloudFlare config file above and reload nginx again.
Check also my post about setting up a cronjob to automatically update the CloudFlare IP addresses.