Nginx restore real IP address when behind a reverse proxy

When Nginx is being used as a reverse proxy, the client’s IP address is usually passed to the backend server in the X-Real-IP header. To restore the real IP address in Nginx, you can add the following configuration to your Nginx server block:

set_real_ip_from proxy_ip;
real_ip_header X-Real-IP;

Where proxy_ip is the IP address of the reverse proxy.

You can also use the proxy_protocol directive in the listen block to enable the proxy protocol and retrieve the client’s IP address.

listen 80 proxy_protocol;

Be sure to reload Nginx after making these changes with

sudo service nginx reload

or

sudo systemctl reload nginx

Also, it depends on the version of Nginx you are using, please check the documentation of your version for more details.

Leave a Comment