nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)

凉白开 Nginx754,9428字数 1830阅读6分6秒阅读模式

nginx模块ngx_http_log_request_speed可以用来找出网站哪些请求很慢,针对站点很多,文件以及请求很多想找出哪些请求比较慢的话,这个插件非常有效.作者的初衷是写给自己用的,用来找出站点中处理时间较长的请求, 这些请求是造成服务器高负载的很大根源. 日志记录之后,在使用perl脚本分析日志,即可知道哪些请求需要修正.

1. 模块安装

nginx第三方模块安装方法,我们ttlsa.com已经说过很多次了,我这边不在重复了。
配置参数文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

./configure --prefix=/usr/local/nginx-1.4.1 --with-http_stub_status_module \
 --add-module=../ngx_http_log_request_speed

2. 指令log_request_speed

log_request_speed_filter
语法: log_request_speed_filter [on|off]
配置段: n/a
context: location, server, http
启动或禁用模块文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

log_request_speed_filter_timeout
语法: log_request_speed_filter_timeout [num sec]
默认: 5秒
配置段: location, server, http
这边并不是真正意义的超时,而是说当请求超过这边给定的时间,将会记录到nginx错误日志中. 默认值是5000微秒(5秒),如果一个请求小于5秒,这个请求不会被记录到日志中,但是如果超过5秒,那请求将会被记录到nginx的错误日志中文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

3. 使用实例

3.1 nginx配置文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

http{
      log_request_speed_filter on;
      log_request_speed_filter_timeout 3;
      ...
}

错误日志中记录的慢请求如下文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

nginx

nginx慢请求日志

3.2 日志分析文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

cd /usr/local/nginx-1.4.1/logs
wget http://wiki.nginx.org/images/a/a8/Log_Analyzer.tar.gz
tar -xzvf Log_Analyzer.tar.gz
cd request_speed_log_analyzer
# cat ../error.log | grep 'process request'| ./analyzer.pl -r
POST /wp-admin/admin-ajax.php HTTP/1.1 --- avg ms: 1182, value count: 2
GET /shmb/1145.html HTTP/1.1 --- avg ms: 2976, value count: 1 <--- THE WINNER

从日志中,我们发现这边有2条请求比较慢,最慢的是/shmb/1145.html ,而且还标示“THE WINNER”,作者你赢了。很幽默。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

3.3 分析脚本语法文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

# ./analyzer.pl -h 
-h : this help message # 显示帮助信息
-u : group by upstream # 按upstream分组
-o : group by host # 按主机分组
-r : group by request # 按请求分组,推荐这个

4. nginx测试版本

目前作者只在0.6.35和0.7.64下测试,不保证其他环境下可以使用。我当前的测试版本是1.4.1,目前使用正常,在使用前请大家先测试一下。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

5. 结束语

首先很感谢作者写的这个简单实用的nginx插件,这个插件的目的不仅仅是记录请求的响应时间,而且是用来找出响应慢的请求。如果你的服务器上有大量的站点,或者大量的程序文件,但是访问量不高,负载却很高,你想找出是哪个请求慢,我想这个插件非常适合你。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

参考地址
ngx_http_log_request_speed下载地址:http://wiki.nginx.org/images/7/78/Ngx_http_log_request_speed.tar.gz
ngx_http_log_request_speed脚本地址:http://wiki.nginx.org/images/a/a8/Log_Analyzer.tar.gz文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

来源站点:运维生存时间  网址:https://www.ttlsa.com/html/3244.html文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/ 文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 30/09/2013 05:10:22
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/
评论  7  访客  2
    • Aceslup
      Aceslup 9

      很感谢分享。

        • 匿名
          匿名 9

          @ Aceslup 全网都找不到Ngx_http_log_request_speed模块下载 = =

      评论已关闭!