如何在 Nginx 日志中记录反向代理上游服务器服务请求? [英] How to record reverse proxy upstream server serving request in Nginx log?
问题描述
我们使用 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屋!