Find user IP address with Cloudflare & Nginx
A content delivery network (CDN) is a distributed network of servers that delivers content, typically webpages, to users based on their geographic location. A CDN would serve you from a less distant location on the network.
CloudFlare provides such a CDN service. A friend recommended me to use CloudFlare around a year or so, and I do not regret accepting.
I noticed considerable performance gain when I switched to CloudFlare.
PING hacklog.mu (126.96.36.199) 56(84) bytes of data. 64 bytes from 188.8.131.52: icmpseq=1 ttl=51 time=114 ms 64 bytes from 184.108.40.206: icmpseq=2 ttl=51 time=115 ms 64 bytes from 220.127.116.11: icmpseq=3 ttl=51 time=113 ms 64 bytes from 18.104.22.168: icmpseq=4 ttl=51 time=113 ms 64 bytes from 22.214.171.124: icmp_seq=5 ttl=51 time=114 ms
--- hacklog.mu ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4004ms rtt min/avg/max/mdev = 113.856/114.509/115.854/0.847 ms
CloudFlare bundles other features, among which the Firewall allows you to set rules for rogue visitors.
Get the user's IP address with Nginx
CloudFlare proxies requests to your webserver and as such, your webserver log would record CloudFlare's IP addresses. In order to obtain the user's IP address in a request, you would need to activate the True-Client-IP Header from CloudFlare's administration panel. However, that requires an Enterprise plan. It is not available in the free service.
There is one workaround using
ngxhttprealip_module in Nginx. It allows a change of the client address to one that is specified in the header field. CloudFlare specifies the same in the
CF-Connecting-IP field. The technical story can be summed up as follows in the http context of Nginx:
set_real_ip_from 126.96.36.199/22; set_real_ip_from 188.8.131.52/22; set_real_ip_from 184.108.40.206/22; set_real_ip_from 220.127.116.11/12; set_real_ip_from 18.104.22.168/18; set_real_ip_from 22.214.171.124/18; set_real_ip_from 126.96.36.199/15; set_real_ip_from 188.8.131.52/13; set_real_ip_from 184.108.40.206/20; set_real_ip_from 220.127.116.11/20; set_real_ip_from 18.104.22.168/20; set_real_ip_from 22.214.171.124/22; set_real_ip_from 126.96.36.199/17; set_real_ip_from 188.8.131.52/21; real_ip_header CF-Connecting-IP; ...
The IP addresses specified are those of CloudFlare and they can be obtained here. I suggest checking the page from time to time for updates or you might even monitor changes on the page (^^,) ...
Ubuntu & openSUSE come bundled with ngxhttprealipmodule. If Nginx is complaining about an unknown directive in your distribution, then you most likely need to compile Nginx with the