Nginx: Get the upstream URI used by proxy_pass
Asked Answered
A

3

10

I am using proxy_pass and would like to log the URI used by proxy_pass.

I can log every detail like $upstream_addr (IP) of the process except the URI itself.

Am I missing something or this is not possible?

Amarelle answered 25/7, 2020 at 15:22 Comment(1)
Possibly my duplicated question πŸ˜…πŸ‘‰ #63696172 – Dasya
P
1

I am using proxy_pass and would like to log the URI used by proxy_pass.

There's $upstream_addr but you still can't get full URI there.

It depends but if your app is already in production, add $upstream_addr and check your upstream's access log.

If your app is still in debugging, use nginx's debug level in error_log directive.

I was hoping to get the full URI since I had a little trouble with my params. Something they seem to be ignored and I tried to log these.

Well, you probably hit by nginx's proxy_pass URI processing rule.

A request URI is passed to the server as follows:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

   location /name/ {
       proxy_pass http://127.0.0.1/remote/;
   }

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:

   location /some/path/ {
       proxy_pass http://127.0.0.1;
   }

If you need to pass full URI to your upstream, just specify your upstream rather than an upstream URI to your proxy_pass, and remove any trailing forward slashes there.

proxy_pass http://backend_upstream;
Plagal answered 3/8, 2020 at 6:23 Comment(1)
Thank you. Removing the URI fixes all my problems. I never figured out why it was breaking when I tried to specify the URI using the $uri variable. But what I needed to do was pass the whole thing along basically. – Samantha
I
0

I believe the variable you would want to use is $proxy_host in the logging format.

Nginx Var Reference

$proxy_host name and port of a proxied server as specified in the proxy_pass directive

Inca answered 30/7, 2020 at 22:35 Comment(1)
Thank you for your answer. The $proxy_host is just the domain name. I was hoping to get the full URI since I had a little trouble with my params. Something they seem to be ignored and I tried to log these. It seems that there isn't any $proxy_request_uri function I could use since it's usual the same as the original one. – Amarelle
S
0

In my first NGINX configurations I struggled with getting a lot of 404 errors from the upstream servers when I believed I should be passing a good URI. I could log the uri my NGINX server received, but was never sure what my NGINX server was passing to the upstream server. In my experience and research I never found a way to log what NGINX passes on the NGINX server that is handling the client request.

BUT, the upstream server knows the complete URI the NGINX server is requesting. In my case the upstream server was handled by another team and I did not have access to the logs on that server. Finally I realized, why not use my own upstream server where I do have access to the logs as well as one I can specify how it logs. I could easily do this because I had more than one NGINX server is my development environmennt. I use the first NGINX server as the proxy server I am configuring, and the second as the upstream server.

I will call the original NGINX server I am configuring "MyProxyServer.com" I will call the upstream server I am using to log the URI that MyProxyServer.com passes "Upstream.com"

On MyProxyServer.com I configured :

location /upstream/ {
    access_log  /etc/nginx/logs/received_requests.log   logformat1;
    proxy_pass  https://Upstream.com/;
}

On Upstream.com I configured :

location /  {
    access_log  /etc/nginx/logs/unexpected_uri.log    logformat1;
    try_files /unexpected.html =123;
}

location /uricapture/  {
    access_log  /etc/nginx/logs/uri_passed.log    logformat1;
    try_files /index.html =234;
}

Using your favorite client, GET https://MyProxyServer.com/uricapture/the/final/endpoint/

On MyProxyServer.com I can examine the log and see that it received a GET request. GET /uricapture/the/final/endpoint/

On Upstream.com I can examine the log, and in the uri_passed.log I see that MyProxyServer.com requested: GET /the/final/endpoint/

I got back the index.html file I had set up.

Sharl answered 14/2, 2023 at 14:51 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.