nginx with real_ip -- log both client remote_addr and proxies address

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:

log_format main '$remote_addr - $remote_user [$time_local] [$msec] '
                '[$request_time] "$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';