大网数据广告活动
服务器运维问答

nginx log full proxy_pass to log

· 52CE 我爱测

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.originalserve…

问题原文

原帖链接 · Alec Ruderman · CC BY-SA 4.0

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"'
                                '}'
                              '}'
                            '}';```

已采纳解答

原帖链接 · notStan · CC BY-SA 4.0

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" "-"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-08-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。