Get real IP-address in NGINX log Debian

Are you using CloudFlare? Then you might have the issue that NGINX registers the IP-address of the CloudFlare hosting platform instead of the IP-address of the visitor.

In that case you have to enable the http-real-ip module. You can do this by running:

apt-get install nginx-extras

After that add the following configuration to the server block:
include /etc/nginx/cloudflare;

# - IPv4
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;

# - IPv6
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
real_ip_header CF-Connecting-IP;

The list of IP-addresses might be incomplete. Here you can find the up-to-date list of CloudFlare IP addresses:

https://www.cloudflare.com/ips/
https://www.cloudflare.com/ips-v4

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp