java nginx:使用 nginx 作为反向代理时,是否可以在访问日志中捕获响应标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4272686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nginx: Is it possible to capture response headers in access log when using nginx as a reverse proxy?
提问by jkndrkn
We are using nginx as a reverse proxy to control and log access to a Clojure (Java) web service application.
我们使用 nginx 作为反向代理来控制和记录对 Clojure (Java) Web 服务应用程序的访问。
We are able to generate an access_log
and capture incoming headers using nginx just fine. Our Clojure app logs activity via log4j. Trouble is, is that we can't match an entry in the access_log
to an entry generated by the app.
我们能够access_log
使用 nginx生成并捕获传入的头文件就好了。我们的 Clojure 应用程序通过 log4j 记录活动。问题是,我们无法将access_log
中的条目与应用程序生成的条目匹配。
The app responds to access by sending response headers as well as a body. We can freely change these response headers. My initial thought was to generate a UUID that corresponds to each and every web service request and send that back to the user within the reply header X-Uuid
. My thought was that I could capture this response by creating a custom log_format
:
该应用程序通过发送响应标头和正文来响应访问。我们可以自由更改这些响应头。我最初的想法是生成一个对应于每个 Web 服务请求的 UUID,然后在回复标头中将其发送回用户X-Uuid
。我的想法是我可以通过创建自定义来捕获此响应log_format
:
log_format lt-custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time $http_x_uuid';
It's looking like nginx can capture headers in incoming requests but not outgoing replies (I verified this by replacing $http_x_uuid
with $http_content_type
).
看起来 nginx 可以捕获传入请求中的标头,但不能捕获传出回复中的标头(我通过替换$http_x_uuid
为验证了这一点$http_content_type
)。
So! Is there a way I can tie my access_log
enties and my log4j entries by capturing outgoing reply headers using nginx? Is there a better way? I'd rather not have to rely on users generating their own UUIDs.
所以!有没有办法access_log
通过使用 nginx 捕获传出的回复标头来绑定我的实体和我的 log4j 条目?有没有更好的办法?我宁愿不必依赖用户生成自己的 UUID。
Thanks so much!
非常感谢!
回答by citrin
$http_x_uuid is header sent by client. Response header send by upstream is $upstream_http_x_uuid
$http_x_uuid 是客户端发送的标头。上游发送的响应头是 $upstream_http_x_uuid
http://wiki.nginx.org/HttpUpstreamModule#.24upstream_http_.24HEADER
http://wiki.nginx.org/HttpUpstreamModule#.24upstream_http_.24HEADER