设置Nginx,Tomcat访问日志记录请求耗时

作者 : 开心源码 本文共1506个字,预计阅读时间需要4分钟 发布时间: 2022-05-12 共132人阅读

一、Nginx通过$upstream_response_time $request_time统计请求和后端服务响应时间

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”'

'$connection $upstream_addr '

'upstream_response_time $upstream_response_time request_time $request_time ';

$request_time和$upstream_response_time之间差别:

$request_time包含了客户数据接收时间,而真正程序的响应时间应该用$upstream_response_time

所以假如客户网络较差,或者者传递数据较大时,$request_time会比$upstream_response_time大很多

详细参考:http://wuzhangshu927.blog.163.com/blog/static/114224687201310674652147/

二、Tomcat通过%D或者%T统计请求响应时间

server.xml使用配置方式

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

prefix=”localhost_access_log.” suffix=”.txt”

pattern=”%h %l %u [%{yyyy-MM-dd HH:mm:ss}t] %{X-Real_IP}i “%r” %s %b %D %F” />

%D – 官方解释:Time taken to process the request, in millis,解决请求的时间,以毫秒为单位

%T – 官方解释:Time taken to process the request, in seconds,解决请求的时间,以秒为单位

%F – 官方解释:Time taken to commit the response, in millis,提交响应的时间,以毫秒为单位

详细说明:http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging

三、通过awk命令辅助统计access.log

1.简单统计nginx访问日志access log每分钟请求数

awk -F: '{count[$2″:”$3]++} END {for (minute in count) print minute, count[minute]}' /usr/local/nginx/logs/access.log | sort > count.log

结果如下所示(count.log)

18:30 2086

18:31 2184

18:32 2176

18:33 2122

18:34 2128

18:35 2179

参考:http://huoding.com/2013/01/26/215

2.统计请求响应时间超过10s的记录

awk '($NF > 10){print $0}' /usr/local/tengine/logs/cut-log/access_2015-01-12.log >t10_0112.log

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 设置Nginx,Tomcat访问日志记录请求耗时

发表回复