nginx - 如何在Nginx日志中记录反向代理上游服务器服务请求?

标签 nginx reverse-proxy

我们使用 Nginx 作为反向代理并进行以下设置:

    upstream frontends {
      server 127.0.0.1:8000;
      server 127.0.0.1:8001;
      server 127.0.0.1:8002;
      [...]
    }
    
    server {
      location / {
        proxy_pass http://frontends;
        [...]
      }
      [...]
    }

作为访问日志的一部分,我想记录已服务请求的上游服务器,在我们的例子中仅意味着关联的本地主机端口。

文档中的变量 ( http://wiki.nginx.org/HttpProxyModule#Variables ) 提到 $proxy_host 和 $proxy_port 但在日志中它们总是以值“frontends”和“80”结束。

最佳答案

首先添加新的日志格式

log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name $host to: $upstream_addr: $request $status upstream_response_time $upstream_response_time msec $msec request_time $request_time';

示例输出:

[18/Nov/2019:10:08:15 -0700] <request IP> - - - <config host> <request host> to: 127.0.0.1:8000: GET /path/requested HTTP/1.1 200 upstream_response_time 0.000 msec 1574096895.474 request_time 0.001

然后将accesslog重新定义为

access_log /var/log/nginx/access.log upstreamlog;

log_format 转到 http {} 部分,access_log 可以位于位置内部。

关于nginx - 如何在Nginx日志中记录反向代理上游服务器服务请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18627469/

相关文章:

go - 如何读取来自 NewSingleHostReverseProxy 的响应

symfony - 如何在 nginx 的子目录中安装 symfony2 应用程序

css - Rails 图像 Assets 未加载

nginx - 如何在 80 以外的其他端口上运行 Nginx

networking - Docker Compose网络:nginx中的主机名无法解析

node.js - Nginx 反向代理映射不重定向

apache - Apache白名单反向代理

php - nginx 本地网络服务器的所有权和权限

php - 无法连接到我的 Ratchet/WebSocket 服务器

apache - 反向代理重定向 http 到 https