Amazon Elastic Load Balancer and Forwarding Real-IP Nginx

This article is for Amazon AWS only. For forwarding visitor real IP in nginx proxy setup, check this.

If you are running nginx on Amazon EC2 instance, behind Amazon Elastic Load Balancer (ELB), for any IP-specific nginx config and/or applicaiton code to work, you need to do following:

Open /etc/nginx/nginx.conf file

Add following inside http {..} block

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

Then just reload nginx for change to take effect.

Security Consideration:

If your nginx EC2 instance can be accessed directly, apart from traffic coming through Elastic Load Balancer, then its better to change:

set_real_ip_from 0.0.0.0/0;

to fixed IP address assigned to Elastic Load Balancer.

If IP address for ELB is 1.2.3.4, then it should look like:

set_real_ip_from 1.2.3.4;

2 responses to “Amazon Elastic Load Balancer and Forwarding Real-IP Nginx”

  1. Thanks. That really helped me out after reading 10 other suggestions that didn’t work 🙂