Nginx $http_x_forwarded_for not always being set

I've noticed in our access logs that sometimes, the $http_x_forwarded_for variable has not been set, and instead, the correct IP is the one set to $remote_addr. Is it possible to set $http_x_forwarded_for as $remote_addr, when it doesn't otherwise exist?

Basically, the following psuedo code, in nginx logic:

if($http_x_forwarded_for == null) $http_x_forwarded_for = $remote_addr

I base some limit req zones on the $http_x_forwarded_for, so I think this issue could be why it doesn't always take effect.

Thanks.


Solution 1:

I'd hazard a guess that you've got ngx_http_realip_module enabled and not all of your proxies' IPs are listed as trusted.