I have an nginx instance set up behind multiple load balancing proxies, and I am using real_ip
to get the clients IP address in remote_addr for processing by my Python application.
However in my logs I would like to log the proxy server that actually made the request, but $remote_addr
has now been overwritten, and I can't find anything that says a copy of it is made.
How would I be able to accomplish this?
Your setup may have changed since you posted this question, but I had the same problem and there is finally a solution available. I'll post it here in case Google brings someone else to this page.
As @Michael Hampton♦ indicates in his answer to my own version of this question, nginx has added the variable
$realip_remote_addr
to the Real_IP module. It holds the original value of$remote_addr
, allowing you to use/log both the originating client's IP and the IP of the server that sent the actual request to nginx. This was added to version 1.9.7, as of November 2015.Don't really get if you want to log at nginx side or inside your application?
For nginx side you can use
$http_x_forwarded_for
in nginx log_format definition, something like this:The module itself seams to not support that. However, reverse proxies (for which real_ip is mostly used) may provide a HTTP header field containing the proxies IP address.
Unfortunately, you cannot check whether the address has been rewritten or not, so a client not connecting through a real_ip-enabled proxy could forge the header field.
Check your proxy's doc, maybe you can use a HTTP header field for the IP address.