nginx記錄分析網(wǎng)站響應(yīng)慢的請(qǐng)求(ngx_http_log_request_speed)
nginx模塊ngx_http_log_request_speed可以用來(lái)找出網(wǎng)站哪些請(qǐng)求很慢,針對(duì)站點(diǎn)很多,文件以及請(qǐng)求很多想找出哪些請(qǐng)求比較慢的話,這個(gè)插件非常有效.作者的初衷是寫給自己用的,用來(lái)找出站點(diǎn)中處理時(shí)間較長(zhǎng)的請(qǐng)求, 這些請(qǐng)求是造成服務(wù)器高負(fù)載的很大根源. 日志記錄之后,在使用perl腳本分析日志,即可知道哪些請(qǐng)求需要修正.
1. 模塊安裝
nginx第三方模塊安裝方法這里就一筆略過(guò)了。
配置參數(shù)
./configure --prefix=/usr/local/nginx-1.4.1 --with-http_stub_status_module / --add-module=../ngx_http_log_request_speed
2. 指令log_request_speed
2.1 log_request_speed_filter
語(yǔ)法:
log_request_speed_filter [on|off]
配置段: n/a
context: location, server, http
啟動(dòng)或禁用模塊
2.2 log_request_speed_filter_timeout
語(yǔ)法:
log_request_speed_filter_timeout [num sec]
默認(rèn): 5秒
配置段: location, server, http
這邊并不是真正意義的超時(shí),而是說(shuō)當(dāng)請(qǐng)求超過(guò)這邊給定的時(shí)間,將會(huì)記錄到nginx錯(cuò)誤日志中. 默認(rèn)值是5000微秒(5秒),如果一個(gè)請(qǐng)求小于5秒,這個(gè)請(qǐng)求不會(huì)被記錄到日志中,但是如果超過(guò)5秒,那請(qǐng)求將會(huì)被記錄到nginx的錯(cuò)誤日志中
3. 使用實(shí)例
3.1 nginx配置
http{ log_request_speed_filter on; log_request_speed_filter_timeout 3; ...} 錯(cuò)誤日志中記錄的慢請(qǐng)求如下
3.2 日志分析
cd /usr/local/nginx-1.4.1/logswget http://wiki.nginx.org/images/a/a8/Log_Analyzer.tar.gztar -xzvf Log_Analyzer.tar.gzcd 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: 2GET /shmb/1145.html HTTP/1.1 --- avg ms: 2976, value count: 1 <--- THE WINNER
從日志中,我們發(fā)現(xiàn)這邊有2條請(qǐng)求比較慢,最慢的是/shmb/1145.html ,而且還標(biāo)示“THE WINNER”,作者你贏了。很幽默。
3.3 分析腳本語(yǔ)法
# ./analyzer.pl -h
- -h : this help message # 顯示幫助信息
- -u : group by upstream # 按upstream分組
- -o : group by host # 按主機(jī)分組
- -r : group by request # 按請(qǐng)求分組,推薦這個(gè)
4. nginx測(cè)試版本
目前作者只在0.6.35和0.7.64下測(cè)試,不保證其他環(huán)境下可以使用。我當(dāng)前的測(cè)試版本是1.4.1,目前使用正常,在使用前請(qǐng)大家先測(cè)試一下。
nginx替換網(wǎng)站響應(yīng)內(nèi)容(ngx_http_sub_module)
ngx_http_sub_module模塊是一個(gè)過(guò)濾器,它修改網(wǎng)站響應(yīng)內(nèi)容中的字符串,比如你想把響應(yīng)內(nèi)容中的‘jb51'全部替換成‘VEVB武林網(wǎng)',這個(gè)模塊已經(jīng)內(nèi)置在nginx中,但是默認(rèn)未安裝,需要安裝需要加上配置參數(shù):--with-http_sub_module
1.指令(Directives)
語(yǔ)法:
sub_filter string replacement;
默認(rèn)值: —
配置段: http, server, location
設(shè)置需要使用說(shuō)明字符串替換說(shuō)明字符串.string是要被替換的字符串,replacement是新的字符串,它里面可以帶變量。
語(yǔ)法:
sub_filter_last_modified on | off;
默認(rèn)值: sub_filter_last_modified off;
配置段: http, server, location
這個(gè)指令在nginx 1.5.1中添加,我這個(gè)版本沒(méi)有,可以忽略掉.
Allows preserving the “Last-Modified” header field from the original response during replacement to facilitate response caching.
By default, the header field is removed as contents of the response are modified during processing.
語(yǔ)法:
sub_filter_once on | off;
默認(rèn)值: sub_filter_once on;
配置段: http, server, location
字符串替換一次還是多次替換,默認(rèn)替換一次,例如你要替換響應(yīng)內(nèi)容中的jb51為VEVB武林網(wǎng),如果有多個(gè)jb51出現(xiàn),那么只會(huì)替換第一個(gè),如果off,那么所有的jb51都會(huì) 被替換
語(yǔ)法:
sub_filter_types mime-type ...;
默認(rèn)值: sub_filter_types text/html;
配置段: http, server, location
指定需要被替換的MIME類型,默認(rèn)為“text/html”,如果制定為*,那么所有的
2. nginx替換字符串實(shí)例
2.1 配置
server { listen 80; server_name m.survivalescaperooms.com; root /data/site/m.survivalescaperooms.com; location / { sub_filter jb51 'VEVB武林網(wǎng)'; sub_filter_types text/html; sub_filter_once on; }} 2.2 測(cè)試
內(nèi)容如下
# cat /data/site/m.survivalescaperooms.com/2013/10/20131001_sub1.html
welcome to jb51!jb51 TEAM!
訪問(wèn)結(jié)果
# curl m.survivalescaperooms.com/2013/10/20131001_sub1.html
welcome to VEVB武林網(wǎng)!jb51 TEAM!
我們可以看到它替換是不區(qū)分大小寫的,而且jb51只被替換了一次。我把sub_filter_once on改成off試試。
location / { sub_filter jb51 'VEVB武林網(wǎng)'; sub_filter_once off;} 接著測(cè)試
# curl m.survivalescaperooms.com/2013/10/20131001_sub1.html
welcome to VEVB武林網(wǎng)!VEVB武林網(wǎng) TEAM!
我們可以看到j(luò)b51都被替換掉了.
例如你想在</head>后追加一段js,配置如下:
location / { sub_filter </head> '</head><script language="javascript" src="$script"></script>'; sub_filter_once on;} 這邊我就不再做測(cè)試了,大家可以測(cè)試一下.




















