Nginx: log the actual forwarded proxy_pass request URI to upstream

2.3k Views Asked by At

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?

2

There are 2 best solutions below

5
AleXoundOS On BEST ANSWER

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.

0
JM Lord On

I have used tcpflow -cg directly inside my NGINX docker. It has a lot of output, but the interesting part is this one:

172.023.000.001.37378-172.023.000.007.00080: GET /react/home.html HTTP/1.1
Host: localhost
[...]

172.023.000.007.49748-172.023.000.002.01234: GET /home.html HTTP/1.0
Host: website-react:1234
[...]

From this you can read: http://localhost/react/home.html --> http://website-react:1234/home.html