Search code examples
nginxservernginx-reverse-proxyproxypassserver-configuration

Nginx: log the actual forwarded proxy_pass request URI to upstream


I've got the following nginx conf:

http {

  log_format upstream_logging '[proxied request] '
                              '$server_name$request_uri -> $upstream_addr';
  
  access_log /dev/stdout upstream_logging;

  server {

    listen 80;
    server_name localhost;
    
    location ~ /test/(.*)/foo {
      proxy_pass http://127.0.0.1:3000/$1;
    }
  }
}

When I hit:

http://localhost/test/bar/foo

My actual output is:

[proxied request] localhost/test/bar/foo -> 127.0.0.1:3000 

While my expected output is:

[proxied request] localhost/test/bar/foo -> 127.0.0.1:3000/bar

Is there a variable or a way to produce the actual proxied URI in the log?


Solution

  • If not production, you can test what is being sent by nginx after launching the simplest listening server on the desired local address and port (instead of a real one):

    $ nc -l 127.0.0.1 3000
    POST /some/uri HTTP/1.0
    Host: 127.0.0.1
    Connection: close
    Content-Length: 14
    
    some payload
    

    Response can be simulated by manually entering HTTP/1.1 200 OK, followed with 2 new lines, while nc is running.