nginx日志request_time 和upstream_response_time区别

nginx常见的2个time

我们在通过tsar采集对nginx的数据进行采集时,发现tsar采集到的rt时间和nginx自身日志中打印的时间$request_time对不上,这让我们在收到报警后很难快速的和nginx的日志对应起来,从而找到我们响应慢的api。于是对nginx的几个处理时间进行了分析,原来$request_time包含了用户数据接收时间,而真正程序的响应时间应该用$upstream_response_time

#tsar采集nginx各字段的含义
    * Accept:总共接收的新连接数目
    * Handle:总共处理的连接数目
    * Reqs:总共产生请求数目
    * Active:活跃的连接数,等于read+write+wait
    * Read:读取请求数据的连接数目
    * Write:向用户写响应数据的连接数目
    * Wait:长连接等待的连接数目
    * Qps:每秒处理的请求数
    * Rt:平均响应时间ms

下面介绍下2者的差别:

request_time

官网描述:

request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client 。

指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出
响应数据时间。

upstream_response_time

官网描述:

keeps times of responses obtained from upstream servers; times are kept in seconds with a milliseconds resolution. Several response times are separated by commas and colons like addresses in the $upstream_addr variable

是指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间。

从上面的描述可以看出,$request_time肯定比$upstream_response_time值大,特别是使用POST方式传递参数时,因为Nginx会把request body缓存住,接受完毕后才会把数据一起发给后端。所以如果用户网络较差,或者传递数据较大时,$request_time会比$upstream_response_time大很多。

所以如果使用nginx的accesslog查看php程序中哪些接口比较慢的话,记得在log_format中加入$upstream_response_time

新版本中的time

除了上述的request_time和upstream_response_time比较常用,在新的Nginx版本中对整个请求各个处理阶段的耗时做了近一步的细分:

$upstream_connect_time(1.9.1):

keeps time spent on establishing a connection with the upstream server (1.9.1); the time is kept in seconds with millisecond resolution. In case of SSL, includes time spent on handshake. Times of several connections are separated by commas and colons like addresses in the $upstream_addr variable.

跟后端server建立连接的时间,如果是到后端使用了加密的协议,该时间将包括握手的时间。

$upstream_header_time(1.7.10):

keeps time spent on receiving the response header from the upstream server (1.7.10); the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable.

接收后端server响应头的时间。

官方对各个时间的解释

地址

NGINX provides a number of built‑in timing variables that you can include in log entries. All are measured in seconds with millisecond resolution.

  • $request_time – Full request time, starting when NGINX reads the first byte from the client and ending when NGINX sends the last byte of the response body
  • $upstream_connect_time – Time spent establishing a connection with an upstream server
  • $upstream_header_time – Time between establishing a connection to an upstream server and receiving the first byte of the response header
  • $upstream_response_time – Time between establishing a connection to an upstream server and receiving the last byte of the response body
原文地址:https://www.cnblogs.com/vinsent/p/12259339.html