Nginx restore real IP address when behind a reverse proxy

Nginx restore real IP address when behind a reverse proxy

To restore the real IP address of clients when Nginx is behind a reverse proxy, you can use the real_ip_header directive in the http block of your Nginx configuration file.

Here's an example of how you can use the real_ip_header directive to restore the real IP address of clients when Nginx is behind a reverse proxy:

refer t‮tual:o‬turi.com
http {
    real_ip_header X-Real-IP;
    set_real_ip_from 10.0.0.0/8;
    set_real_ip_from 172.16.0.0/12;
    set_real_ip_from 192.168.0.0/16;

    # Your other http block directives go here
}

In this example, the real_ip_header directive specifies that the X-Real-IP header should be used to restore the real IP address of clients. The set_real_ip_from directives specify the range of IP addresses that the reverse proxy is expected to come from.

The X-Real-IP header should be set by the reverse proxy to the real IP address of the client. When Nginx receives a request from the reverse proxy, it will use the value of the X-Real-IP header to set the $remote_addr variable, which can be used in your Nginx configuration to access the real IP address of the client.

For more information on the real_ip_header and set_real_ip_from directives, you can refer to the Nginx documentation.

Created Time:2017-10-30 10:17:52  Author:lautturi