nginx开启记录请求时间

文章详细解释了Nginx配置中的log_format,特别是$request_time和$upstream_response_time两个时间指标,前者表示从接收到请求到发送完整响应的时间,后者记录与上游服务器交互的时间。这两个变量在处理POST请求和大数据量传输时尤为关键,因为Nginx会缓存请求体,导致$request_time可能显著大于$upstream_response_time。
摘要由CSDN通过智能技术生成

修改nginx配置如下,reload生效,/usr/local/nginx/sbin/nginx -s reload

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for "|$request_time|$upstream_response_time'; 

    access_log  logs/access.log  main;

配置说明:

(1)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 。
 
指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出
响应数据时间。
 
(2)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大很多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值