7

Given the following location with proxy_pass, I want to log the fully qualified uri.

location ~* ^/?(foo/?.*$) {
    proxy_pass https://www.someserver.com/some-thing-else/$1;
    break;
}

So for example I'd like

https://www.originalserver.com/foo?page=5

to redirect to

https://www.someserver.com/some-thing-else/foo?page=5

which I believed to be working; I think something else later down the chain is dropping the query_string.

So I want to output to the log file exactly what it was passed to. I tried $uri, $proxy_host, and $upstream_addr but I couldn't find anything that would return the fully qualified uri

https://www.someserver.com/some-thing-else/foo?page=5

Instead I get:

proxy_host:  www.someserver.com 
upstream_addr:   123.45.67.890:443  
uri_path:    /foo   
uri_query:   page=5 

I'm really new to rewrite rules, and coming from microsoft, so I've been reading the comprehensive docs for nginx and the searching the web, but I can't find an answer for this. Any help would be greatly appreciated. Thanks.

Addendum: this is the only rule of 7 where I'm running into issues.

Addendum Addendum: This is our current log format.

log_format json escape=json '{'
                              '"time_local": "$time_local",'
                              '"core": {'
                                '"site": "$server_name",'
                                '"server": "$host",'
                                '"dest_port": "$server_port",'
                                '"src_ip": "$realip_remote_addr",'
                                '"status": "$status",'
                                '"protocol": "$server_protocol",'
                                '"body_bytes_sent": "$body_bytes_sent",'
                                '"remote_addr": "$remote_addr",'
                                '"remote_user": "$remote_user",'
                                '"request": "$request",'
                                '"nginx_version": "$nginx_version",'
                                '"http": {'
                                  '"http_referer": "$http_referer",'
                                  '"http_user_agent": "$http_user_agent",'
                                  '"http_x_header": "$http_x_header",'
                                  '"uri_query": "$query_string",'
                                  '"uri_path": "$uri",'
                                  '"http_method": "$request_method",'
                                  '"response_time": "$upstream_response_time",'
                                  '"cookie": "$http_cookie",'
                                  '"request_time": "$request_time",'
                                  '"http_x_forwarded_for": 
                                  '"$http_x_forwarded_for",'
                                  '"proxy_host": "$proxy_host",'
                                  '"upstream_addr": "$upstream_addr"'
                                '}'
                              '}'
                            '}';```
suspectus
  • 658
  • 1
  • 5
  • 11
Alec Ruderman
  • 73
  • 1
  • 1
  • 5

1 Answers1

4

To log the full request uri you can change your log_format inside of the http block. Change it to:

log_format  main  '$remote_addr - $remote_user [$time_local] "$host$request_uri" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

sample output:

1.1.1.1 - - [02/Aug/2019:20:32:36 +0200] "www.website.eu/index.php?param=some_param" 200 1238 "https://www.website.eu/" "Mozilla/5.0" "-"
notStan
  • 323
  • 2
  • 9
  • I see where it returns the original request, $host$request_uri, but not where it returns the full proxy_pass. $http_referer is coming back blank in our logs. This could be indicative of a bigger issue. I'm a developer doing application specific rewrites while devops is doing sitewide nginx configuration work. – Alec Ruderman Aug 02 '19 at 18:54
  • If nginx doesn't log it you can log it with the application you are running on the reverse proxy. Also if the `http_referrer` is empty it is simply never sent to nginx. – notStan Aug 02 '19 at 18:56