之前部署了quic的集群在aws,在测试的时候发现在大报文的情形下HTTP3的请求耗时比较不稳定,并且耗时比普通的HTTP2要大很多,就想看看请求的具体耗时有多少
请求响应日志记录
我的quic集群是通过nginx部署的,所有的请求都是由nginx转发的,只要在nginx里记录请求日志就可以了,通过修改nginx配置文件nginx.conf里的log_format指令参数就可以做到
log_format
指令是用来控制nginx如何记录http请求, 默认的nginx记录日志格式是注释掉的,如果要记录额外的信息,需要自定义log_format格式
我的目的是为了记录请求的耗时,就可以添加如下的log_format格式
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;
修改配置后,nginx的访问日志如下:
几个时间变量的意义:
- $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
$request_time : 就是指从nginx接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应时间等
$upstream_response_time: 是指从nginx向后端建立连接开始到接收到程序处理后的响应的最后一个字节的时间
有了请求响应的日志,我们就可以借助awk文本处理命令来统计access.log
统计nginx访问日志access.log的每分钟请求数
扫描二维码关注公众号,回复:
12569709 查看本文章
awk -F: '{count[$2":"$3]++} END {for (minute in count) print minute, count[minute]}' ./access.log | sort > count.log
统计请求响应时间超过10秒的记录
awk '($NF > 10){print $0}' ./access.log > request_time_gt_10.log
日志分割
1.编写shell脚本
nginx_log_task.sh
#!/bin/bash
#设置日志文件存放目录
logs_path="/opt/server/nginx/logs/"
#设置pid文件
pid_path="/opt/server/nginx/logs/nginx.pid"
#重命名日志文件
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
#重命名异常文件
mv ${logs_path}error.log ${logs_path}error_$(date -d "yesterday" +"%Y%m%d").log
#发送kill -USR1信号给nginx的主进程号,让nginx重新生成一个新的日志文件
kill -USR1 `cat ${
pid_path}`
#将5天前的日志删除
find /opt/server/nginx/logs/ -mtime +5 -name "*.log" -exec rm -rf {
} \;
2.添加定时任务
crontab -e
#每天凌晨00:00定时执行这个脚本
00 00 * * * /opt/nginx_log_task.sh
参考资料:
Using NGINX Logging for Application Performance Monitoring