Nginx Real IP When Using a Load Balancer

If you’re using a load balancing service like the AWS Elastic Load Balancer, your log files are probably populated with every request coming from the same IP - your load balancer. Here’s a quick fix to get the real IP from your users.

Open up /etc/nginx/nginx.conf and add the following text just after your http block opens:

real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;

If your server can be accessed outside of the load balancer, change that 0.0.0.0/0 to the IP address of your load balancer:

set_real_ip_from 123.123.123.123;

After this is done just reload nginx:

sudo nginx -s reload
© 2014 - 2021 · Home ·