Getting real IP while using nginx working behind other nginx



nginx logoFor some reasons you might encounter a situation when you have one nginx working as reverse proxy behind other nginx server. So users send a request to nginx 1, and it sends it to nginx 2. The remote IP of request source as nginx 2 gets is is the IP of nginx 1, and not IP of the user. Here’s how you can fix it. On frontend server (nginx 1) you have to set adding headers:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Make sure nginx you have on backend server was built with http_realip_module. You can check it this way:

nginx -V

And add to you nginx 2 configuration these lines:

set_real_ip_from <here goes frontend server IP>;
real_ip_header X-Forwarded-For;

Now nginx on backend will replace all remote IP from frontend server with the one from X-Forwarded-For HTTP header. If you need more information about ngx_http_realip_module you can get it on nginx oficial site.

Leave a Reply