【问题标题】:How to see the resolved proxy_pass URL in Nginx?如何在 Nginx 中查看已解析的 proxy_pass URL?
【发布时间】:2021-11-26 20:01:24
【问题描述】:

我有一个这样的设置来反映请求:

location ~ ^/prefix/string/(.*)$
{
   proxy_pass https://abc.example.com/$1;
   access_log /var/log/nginx/unique.log logformat
}
  1. 访问我的网站/prefix/string/more_stuff
  2. 日志显示 404,$upstream_addr 仅包含abc.example.com 的 IP, 不是完整的网址
  3. 日志行显示在 unique.log 中,所以我知道我正在点击这个位置块。
  4. 我想消除带有$1 的URL 构造错误的可能性。
  5. 我希望能够记录已解决完整的proxy_pass URL,这样我就可以 修复它或继续进行其他类型的调试。

注意:我意识到在这种情况下我可以阅读文档来找出 nginx 的行为。但如果可能的话,只记录解析的 URL 会更快。

问题:对于这样的proxy_pass,如何记录 NGINX 实际访问的解析 URL?

【问题讨论】:

    标签: nginx proxy nginx-config


    【解决方案1】:

    您可以使用named capture group 将您的URI 后缀捕获为一些唯一变量,而不是$1

    location ~ ^/prefix/string/(?<suffix>.*)$ {
        access_log /var/log/nginx/unique.log unique;
        proxy_pass https://abc.example.com/$suffix;
    }
    

    http 上下文级别定义新的unique 日志格式(例如,将此$suffix 值添加到默认的combined 日志格式):

    # should be defined outside the server block!
    log_format  unique  '$remote_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"'
                        ' Proxy path: "$suffix"';
    server {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2020-09-11
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多