nginx监控来自上游服务器的响应

时间:2017-07-15 04:42:36

标签: nginx reverse-proxy openresty

我有一个使用nginx的反向代理设置。

Client ------> Nginx ------------------------------------> Backend Server
       <------       <-----------------------------------
                      (I want to see the requests here)

如何将http请求(包括从后端服务器发送到nginx的标头)记录到文件中?

也许nginx http proxy module中的一条指令可以帮助我做到这一点。

但我找不到任何有用的指示。

2 个答案:

答案 0 :(得分:2)

注意:我在OP添加标记openresty之前给出了这个答案。我不是Openresty的专家,但我确信在香草Nginx的情况下我给出了正确的细节。

A&#34;后端服务器&#34;被称为&#34;上游&#34;在Nginx术语中。还有一个separate page in the documentation列出了与上游相关的变量。其中,您可能会对

感兴趣
  • $upstream_addr - 处理请求的后端服务器的IP地址
  • $upstream_connect_time$upstream_header_time$upstream_response_time - Nginx等待连接接受的时间,来自上游的标头以及后端处理请求需要多长时间
  • $upstream_http_*NAME*将包含响应标头*NAME*。例如,$upstream_http_etag$upstream_http_last_modified
  • 还有其他变量可用于报告缓存状态,重试等。

将这些记录到Nginx日志文件中是很常见的做法。您需要声明自己的日志格式,例如:

log_format my_upstream '$remote_addr [$time_local] "$request" $status'
  '"$upstream_addr" $upstream_response_time $upstream_http_etag';

然后在locationserver

中使用它
access_log /var/log/nginx/upstream.log my_upstream;

答案 1 :(得分:0)

如果您可以接受OpenResty:

如果要将其包含在日志中,您可以在https://github.com/openresty/lua-nginx-module#body_filter_by_lua_block内重建一个完整的正文。 当指定eof标志时,您可以获得所有响应的标题(https://github.com/openresty/lua-nginx-module#ngxrespget_headers

Ten在Lua中设置一个变量并在log_format中使用它。

注意 - 您的访问日志文件可能很大!

提示 - 您可以使用单独的access_log和log_format进行响应记录。

相关问题