如何在 Nginx 日志中记录反向代理上游服务器服务请求? [英] How to record reverse proxy upstream server serving request in Nginx log?

查看:68
本文介绍了如何在 Nginx 日志中记录反向代理上游服务器服务请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 Nginx 作为此设置的反向代理:

We use Nginx as a reverse proxy with this setup:

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;
    [...]
  }
  [...]
}

作为访问日志的一部分,我想记录为请求提供服务的上游服务器,在我们的例子中只是指相关的本地主机端口.

As part of the access log, I would like to record the upstream server that has served the request, which in our case just means the associated localhost port.

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

The variables in the documentation (http://wiki.nginx.org/HttpProxyModule#Variables) mention $proxy_host and $proxy_port but in the log they always end up with the values "frontends" and "80".

推荐答案

使用 $upstream_addr 你会得到,例如 127.0.0.1:8000>unix:/home/my_user/www/my_site/tmp/.unicorn.sock

Use $upstream_addr and you will get, for example, 127.0.0.1:8000 or unix:/home/my_user/www/my_site/tmp/.unicorn.sock

这篇关于如何在 Nginx 日志中记录反向代理上游服务器服务请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆