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/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?<p>從日志中,我們發(fā)現(xiàn)這邊有2條請(qǐng)求比較慢,最慢的是/shmb/1145.html ,而且還標(biāo)示“the winner”,作者你贏了。很幽默。<br>3.3 分析腳本語(yǔ)法</p><pre class="brush:bash;">#?./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’全部替換成‘本站’,這個(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 “l(fā)ast-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為本站,如果有多個(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?www.jb51.net; ? ??root?/data/site/www.jb51.net;?? ? ??location?/?{ ????sub_filter?jb51?'本站'; ????sub_filter_types?text/html; ????sub_filter_once?on; ??} }
2.2 測(cè)試
內(nèi)容如下
#?cat?/data/site/www.jb51.net/2013/10/20131001_sub1.html
welcome?to?jb51! jb51?team!
訪問(wèn)結(jié)果
#?curl?www.jb51.net/2013/10/20131001_sub1.html
?
welcome?to?本站! jb51?team!
我們可以看到它替換是不區(qū)分大小寫(xiě)的,而且jb51只被替換了一次。我把sub_filter_once on改成off試試。
location?/?{ ??sub_filter?jb51?'本站'; ??sub_filter_once?off; }
接著測(cè)試
#?curl?www.jb51.net/2013/10/20131001_sub1.html
welcome?to?本站! 本站?team!
我們可以看到j(luò)b51都被替換掉了.
例如你想在后追加一段JS,配置如下:
location?/?{ ??sub_filter????'<script></script>'; ??sub_filter_once?on; }