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?

  • 1
    You can only do this if you do not use the real ip module. Then your Python app has to implement the functionality. Mar 10 '13 at 4:13

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.


Dont 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"';
  • 2
    This doesn't work if you use the real ip module, at that point $remote_addr contains the same thing as X-Forwarded-For. I want to log the proxy servers address, and send the real IP on to the remote server as REMOTE_ADDR on uwsgi.
    – X-Istence
    Mar 12 '13 at 6:26

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.

  • No such luck unfortunately. The proxy in this case doesn't have a setting to have it forward the incoming address as a header :/
    – X-Istence
    Mar 12 '13 at 6:27
  • Then you might want to emulate real_ip's behaviour by setting the variables yourself. Maybe a nicely placed if with a simple set will do the trick.
    – Lukas
    Mar 12 '13 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.