Search code examples
nginxpuma

How to keep track of an upstream when nginx has multiple upstreams?


upstream app_server {
  server unix: server1
}

upstream app_server_new {
  server unix: server2
}

server {
  location ^~ /about {
    proxy_pass http://app_server_new
  }
  location @app {
    proxy_pass http://app_server
  }
}

So when the user hits /about, the server redirects to upstream app_server_new.

Now I have a development.log file for puma. But that doesn't tell to which upstream the redirect went. Is there any way by which I can know if the redirect actually works, like keeping a log about hits to that upstream?


Solution

  • You can define custom log for that. Use the fields of your choice:

    log_format upstream '$remote_addr - $upstream_addr - $request - $upstream_response_time - $request_time';

    Then use it in the context specific to your needs:

    access_log /var/log/nginx/upstream.log upstream;

    More information can be found there:
    http://nginx.org/en/docs/http/ngx_http_log_module.html
    http://nginx.org/en/docs/http/ngx_http_upstream_module.html