Search code examples
node.jshttpnginxipx-forwarded-for

NGINX does not set X-Forwarded-For properly


I have an NGINX server acting as a load-balancer for several other servers. Since I need access to the user's IP address, I added the following lines to my nginx configuration:

 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Then, in the backend servers, I simply use the first IP address in the X-Forwarded-For header. However, every few minutes (once every ~2K requests), I get unknown, xxx.xxx.xxx.xxx as the value of the header, where xxx.xxx.xxx.xxx is an IP address (a different one every time). What could be the source of this problem?


Solution

  • From the documentation, The $proxy_add_x_forwarded_for variable consists of:

    the “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma.

    If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.

    From this we can conclude that nginx received a request from some remote client (possibly a forward proxy) with a request header of:

    X-Forwarded-For: unknown