nginx模塊ngx_http_log_request_speed可以用來找出網站哪些請求很慢,針對站點很多,文件以及請求很多想找出哪些請求比較慢的話,這個插件非常有效.作者的初衷是寫給自己用的,用來找出站點中處理時間較長的請求, 這些請求是造成服務器高負載的很大根源. 日志記錄之后,在使用perl腳本分析日志,即可知道哪些請求需要修正.
1. 模塊安裝
nginx第三方模塊安裝方法,我們ttlsa.com已經說過很多次了,我這邊不在重復了。
配置參數
./configure --prefix=/usr/local/nginx-1.4.1 --with-http_stub_status_module \
--add-module=../ngx_http_log_request_speed
1
2
./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
啟動或禁用模塊
log_request_speed_filter_timeout
語法: log_request_speed_filter_timeout [num sec]
默認: 5秒
配置段: location, server, http
這邊并不是真正意義的超時,而是說當請求超過這邊給定的時間,將會記錄到nginx錯誤日志中. 默認值是5000微秒(5秒),如果一個請求小于5秒,這個請求不會被記錄到日志中,但是如果超過5秒,那請求將會被記錄到nginx的錯誤日志中
3. 使用實例
3.1 nginx配置
http{
log_request_speed_filter on;
log_request_speed_filter_timeout 3;
...
}
1
2
3
4
5
http{
log_request_speed_filteron;
log_request_speed_filter_timeout3;
...
}
錯誤日志中記錄的慢請求如下
nginx慢請求日志
3.2 日志分析
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
1
2
3
4
5
6
7
cd/usr/local/nginx-1.4.1/logs
wgethttp://wiki.nginx.org/images/a/a8/Log_Analyzer.tar.gz
tar-xzvfLog_Analyzer.tar.gz
cdrequest_speed_log_analyzer
# cat ../error.log | grep 'process request'| ./analyzer.pl -r
POST/wp-admin/admin-ajax.phpHTTP/1.1---avgms:1182,valuecount:2
GET/shmb/1145.htmlHTTP/1.1---avgms:2976,valuecount:1
從日志中,我們發現這邊有2條請求比較慢,最慢的是/shmb/1145.html ,而且還標示“THE WINNER”,作者你贏了。很幽默。
3.3 分析腳本語法
# ./analyzer.pl -h
-h : this help message # 顯示幫助信息
-u : group by upstream # 按upstream分組
-o : group by host # 按主機分組
-r : group by request # 按請求分組,推薦這個
1
2
3
4
5
# ./analyzer.pl -h
-h:thishelpmessage# 顯示幫助信息
-u:groupbyupstream# 按upstream分組
-o:groupbyhost# 按主機分組
-r:groupbyrequest# 按請求分組,推薦這個
4. nginx測試版本
目前作者只在0.6.35和0.7.64下測試,不保證其他環境下可以使用。我當前的測試版本是1.4.1,目前使用正常,在使用前請大家先測試一下。
5. 結束語
首先很感謝作者寫的這個簡單實用的nginx插件,這個插件的目的不僅僅是記錄請求的響應時間,而且是用來找出響應慢的請求。如果你的服務器上有大量的站點,或者大量的程序文件,但是訪問量不高,負載卻很高,你想找出是哪個請求慢,我想這個插件非常適合你。
參考地址
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