如何查看Nginx中proxy_pass最终解析的完整上游请求URL?
实现打印完整上游请求URL的配置方法
1. 新增自定义日志格式
在Nginx全局http配置块(通常位于nginx.conf文件中,所有server块外层)新增如下日志格式定义:
log_format proxy_debug '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "Upstream-URL: $proxy_scheme://$proxy_host$proxy_uri" ' 'Upstream-IP: $upstream_addr';
其中$proxy_scheme、$proxy_host、$proxy_uri都是Nginx执行完规则后实际生成的上游请求参数,拼接后就是proxy_pass最终请求的完整URL,无需人工推断规则逻辑。
2. 修改对应location的日志配置
调整你的location块的日志配置,指定使用新增的proxy_debug格式输出日志,你可以选择替换原有access_log配置,也可以额外新增一行专门输出调试日志:
location ~ ^/prefix/string/(.*)$ { proxy_pass https://abc.example.com/$1; # 新增调试日志,原有日志配置可以保留不影响业务 access_log /var/log/nginx/unique.log proxy_debug; access_log /var/log/nginx/unique.log logformat; }
3. 重载Nginx配置生效
先执行配置语法校验:
nginx -t
确认返回syntax is ok、test is successful后,执行重载命令让配置生效:
nginx -s reload
问题排查方法
重载完成后再次访问/prefix/string/more_stuff,打开/var/log/nginx/unique.log找到对应请求条目,直接查看Upstream-URL字段的取值即可确认$1拼接是否正确,快速定位404原因。
内容的提问来源于stack exchange,提问作者fastmultiplication
相关产品推荐
相关产品推荐

