Search code examples
nginxreverse-proxy

How to record reverse proxy upstream server serving request in Nginx log?


We use Nginx as a reverse proxy with this setup:

    upstream frontends {
      server 127.0.0.1:8000;
      server 127.0.0.1:8001;
      server 127.0.0.1:8002;
      [...]
    }
    
    server {
      location / {
        proxy_pass http://frontends;
        [...]
      }
      [...]
    }

As part of the access log, I would like to record the upstream server that has served the request, which in our case just means the associated localhost port.

The variables in the documentation (http://wiki.nginx.org/HttpProxyModule#Variables) mention $proxy_host and $proxy_port but in the log they always end up with the values "frontends" and "80".


Solution

  • Use $upstream_addr and you will get, for example, 127.0.0.1:8000 or unix:/home/my_user/www/my_site/tmp/.unicorn.sock