配置nginx,Tomcat日志记录请求耗时

由于公司的业务比较特殊,对速度比较在意,客户最近反应我们的平台时间比较久,处理一个请求十秒左右才返回,领导要求找出原因,我想让nginx日志记录请求处理用了多长时间,后端处理用了多长时间,总共用了多长时间,哪里出现的瓶颈好进行针对性解决

配置nginx统计请求和后端服务Tomcat服务响应时间

编辑nginx的配置文件nginx.conf

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

  

上面的日志是我找的默认形式的,在你原先的日志基础就加入两点即可

$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向后端建立连接开始到后端处理完数据然后关闭连接为止的时间

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

猜你喜欢

转载自www.cnblogs.com/LuckWJL/p/9086823.html