nginx:当使用nginx作为反向代理时,是否可能捕获访问日志中的响应头? [英] nginx: Is it possible to capture response headers in access log when using nginx as a reverse proxy?
问题描述
我们使用nginx作为反向代理来控制和日志访问Clojure(Java)Web服务应用程序。
We are using nginx as a reverse proxy to control and log access to a Clojure (Java) web service application.
我们能够生成一个 access_log
并使用nginx捕获传入的头部很好。我们的Clojure应用程序通过log4j记录活动。麻烦的是,我们不能匹配 access_log
中的条目到应用程序生成的条目。
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.
应用程序通过发送响应标头以及正文来响应访问。我们可以随意改变这些响应头。我最初的想法是生成对应于每个Web服务请求的UUID,并将其发送回用户在回复标头 X-Uuid
中。我的想法是,我可以通过创建一个自定义 log_format
捕获此响应:
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
:
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';
看起来像nginx可以捕获入站请求中的头,但不是出站应答(我通过替换 $ http_x_uuid
与 $ http_content_type
)。
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绑定我的 access_log
enties和我的log4j条目?有更好的方法吗?
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.
非常感谢!
推荐答案
$ http_x_uuid是客户端发送的头。
上游发送的响应头为$ upstream_http_x_uuid
$http_x_uuid is header sent by client. Response header send by upstream is $upstream_http_x_uuid
http://wiki.nginx.org/HttpUpstreamModule#.24upstream_http_.24HEADER
这篇关于nginx:当使用nginx作为反向代理时,是否可能捕获访问日志中的响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!