溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-04-29 16:18:09 來源:億速云 閱讀:181 作者:iii 欄目:大數(shù)據(jù)

今天小編給大家分享一下Nginx記錄分析響應(yīng)慢的請求及替換網(wǎng)站響應(yīng)內(nèi)容怎么配置的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。


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
啟動(dòng)或禁用模塊
2.2 log_request_speed_filter_timeout
語法:

log_request_speed_filter_timeout [num sec]

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

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

錯(cuò)誤日志中記錄的慢請求如下

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 <--- the winner

從日志中,我們發(fā)現(xiàn)這邊有2條請求比較慢,最慢的是/shmb/1145.html ,而且還標(biāo)示“the winner”,作者你贏了。很幽默。
3.3 分析腳本語法

# ./analyzer.pl -h
  • -h : this help message # 顯示幫助信息

  • -u : group by upstream # 按upstream分組

  • -o : group by host # 按主機(jī)分組

  • -r : group by request # 按請求分組,推薦這個(gè)

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模塊是一個(gè)過濾器,它修改網(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)
語法:    

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
這個(gè)指令在nginx 1.5.1中添加,我這個(gè)版本沒有,可以忽略掉.
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為億速云,如果有多個(gè)jb51出現(xiàn),那么只會(huì)替換第一個(gè),如果off,那么所有的jb51都會(huì) 被替換
語法:

 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都被替換掉了.
例如你想在</head>后追加一段js,配置如下:

location / {
  sub_filter   </head> '</head><script language="javascript" src="$script"></script>';
  sub_filter_once on;
}

以上就是“Nginx記錄分析響應(yīng)慢的請求及替換網(wǎng)站響應(yīng)內(nèi)容怎么配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI