Nginx記錄分析響應(yīng)慢的請求及替換網(wǎng)站響應(yīng)內(nèi)容怎么配置

1. 模塊安裝
nginx第三方模塊安裝方法這里就一筆略過了。
配置參數(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
語法:

?log_request_speed_filter?[on|off]

配置段: n/a
context: location, server, http
啟動或禁用模塊
2.2 log_request_speed_filter_timeout
語法:

log_request_speed_filter_timeout?[num?sec]

默認(rèn): 5秒
配置段: location, server, http
這邊并不是真正意義的超時,而是說當(dāng)請求超過這邊給定的時間,將會記錄到nginx錯誤日志中. 默認(rèn)值是5000微秒(5秒),如果一個請求小于5秒,這個請求不會被記錄到日志中,但是如果超過5秒,那請求將會被記錄到nginx的錯誤日志中
3. 使用實(shí)例
3.1 nginx配置

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

錯誤日志中記錄的慢請求如下

Nginx記錄分析響應(yīng)慢的請求及替換網(wǎng)站響應(yīng)內(nèi)容怎么配置

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條請求比較慢,最慢的是/shmb/1145.html ,而且還標(biāo)示“the winner”,作者你贏了。很幽默。<br>3.3 分析腳本語法</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 # 按請求分組,推薦這個

4. nginx測試版本

目前作者只在0.6.35和0.7.64下測試,不保證其他環(huán)境下可以使用。我當(dāng)前的測試版本是1.4.1,目前使用正常,在使用前請大家先測試一下。

nginx替換網(wǎng)站響應(yīng)內(nèi)容(ngx_http_sub_module)
ngx_http_sub_module模塊是一個過濾器,它修改網(wǎng)站響應(yīng)內(nèi)容中的字符串,比如你想把響應(yīng)內(nèi)容中的‘jb51’全部替換成‘本站’,這個模塊已經(jīng)內(nèi)置在nginx中,但是默認(rèn)未安裝,需要安裝需要加上配置參數(shù):–with-http_sub_module
1.指令(directives)
語法:????

sub_filter?String?replacement;

默認(rèn)值:???? —
配置段:???? http, server, location
設(shè)置需要使用說明字符串替換說明字符串.string是要被替換的字符串,replacement是新的字符串,它里面可以帶變量。
語法:????

sub_filter_last_modified?on?|?off;

默認(rèn)值: sub_filter_last_modified off;
配置段:???? http, server, location
這個指令在nginx 1.5.1中添加,我這個版本沒有,可以忽略掉.
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.
語法:

?sub_filter_once?on?|?off;

默認(rèn)值: sub_filter_once on;
配置段: http, server, location
字符串替換一次還是多次替換,默認(rèn)替換一次,例如你要替換響應(yīng)內(nèi)容中的jb51為本站,如果有多個jb51出現(xiàn),那么只會替換第一個,如果off,那么所有的jb51都會 被替換
語法:

?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 測試
內(nèi)容如下

#?cat?/data/site/www.jb51.net/2013/10/20131001_sub1.html
welcome?to?jb51! jb51?team!

訪問結(jié)果

#?curl?www.jb51.net/2013/10/20131001_sub1.html

?

welcome?to?本站! jb51?team!

我們可以看到它替換是不區(qū)分大小寫的,而且jb51只被替換了一次。我把sub_filter_once on改成off試試。

location?/?{ ??sub_filter?jb51?'本站'; ??sub_filter_once?off; }

接著測試

#?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; }

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊14 分享