nginx-request_time和upstream_response_time的区别
定义
一个正常的请求过程如下:
- 用户发出请求
- 建立NGINX连接
- 发送响应
- 接收程序的响应数据
- 关闭NGINX连接
$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.
指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应数据时间。 对于此部分对应的是1+2+3+4+5
。
$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)建立连接开始到接受完数据然后关闭连接为止的时间。 对于此部分对应的是2+3+4+5
。
从上面的描述可以看出,
$request_time
肯定大于等于$upstream_response_time
,特别是使用POST方式传递参数时,因为Nginx会把request body
缓存住,接受完毕后才会把数据一起发给后端。所以如果用户网络较差,或者传递数据较大时,$request_time
会比$upstream_response_time
大很多。如果要从access_log中查看较慢的接口的话,可以在
log_format
中加入$upstream_response_time
。
分析
从网上看到下列一组图片,感觉画的很形象。
request_time
是从接收到客户端的第一个字节开始,到把所有的响应数据都发送完为止。
upstream_response_time
是从与后端建立TCP连接开始到接收完响应数据并关闭连接为止。
所以,request_time
会大于等于upstream_response_time
。
性能优化
对于较高的request_time
很可能是由于连接速度较慢的客户端造成的,对此无能为力,但是较高的request_time
并不代表服务器或者程序的性能不够。
总体来说,没必要在request_time
上花费较多时间,而是应该重点关注upstream_response_time
。
nginx log_format
Nginx 对应的格式
转载
1.https://zhuanlan.zhihu.com/p/438152265?utm_id=0