首頁 > 運維 > Nginx > 主體

Nginx記錄分析響應慢的請求及替換網站回應內容怎麼配置

王林
發布: 2023-05-12 20:16:12
轉載
2029 人瀏覽過


1. 模組安裝
nginx第三方模組安裝方法這裡就一筆略過了。
設定參數

./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]
登入後複製

預設: 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;
   ...
}
登入後複製

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

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 <--- the winner
登入後複製

從日誌中,我們發現這邊有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 # 按請求分組,推薦這個

4. nginx測試版本

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

nginx替換網站回應內容(ngx_http_sub_module)
ngx_http_sub_module模組是一個過濾器,它修改網站回應內容中的字串,例如你想把回應內容中的'jb51 '全部替換成'本站',這個模組已經內建在nginx中,但是預設未安裝,需要安裝需要加上設定參數:--with-http_sub_module

1.指令(directives)
語法:    

sub_filter string replacement;
登入後複製

預設值:     —

設定段:     http, server, location
設定設定使用說明字串取代說明字串.string是要被替換的字串, replacement是新的字串,它裡面可以帶變數。
語法:    

sub_filter_last_modified on | off;
登入後複製

預設值: sub_filter_last_modified off;

設定段:     http, server, location
這個指令在nginx 1.5.1中加入,我這個版本沒有,可以忽略掉.
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 default, the header field is removed as contents of the response areces modified :

 sub_filter_once on | off;
登入後複製

預設值: sub_filter_once on;

設定段: http, server, location

字串替換一次還是多次替換,預設替換一次,例如你要替換回應內容中的jb51為本站,如果有多個jb51出現,那麼只會替換第一個,如果off,那麼所有的jb51都會被替換
語法:

 sub_filter_types mime-type ...;
登入後複製

預設值: sub_filter_types text/html;

配置段: http, server, location

指定需要被替換的mime類型,預設為“text/html”,如果制定為*,那麼所有的

2. nginx替換字串實例
2.1 設定

server {
  listen    80;
  server_name www.jb51.net;
 
  root /data/site/www.jb51.net;  
 
  location / {
    sub_filter jb51 &#39;本站&#39;;
    sub_filter_types text/html;
    sub_filter_once on;
  }
}
登入後複製

2.2 測試

內容如下

# cat /data/site/www.jb51.net/2013/10/20131001_sub1.html
登入後複製
welcome to jb51!
jb51 team!
登入後複製
存取結果

# curl www.jb51.net/2013/10/20131001_sub1.html
登入後複製
登入後複製

#

welcome to 本站!
jb51 team!
登入後複製

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

location / {
  sub_filter jb51 &#39;本站&#39;;
  sub_filter_once off;
}
登入後複製

接著測試

# curl www.jb51.net/2013/10/20131001_sub1.html
登入後複製
登入後複製
welcome to 本站!
本站 team!
登入後複製

我們可以看到jb51都被替換掉了.

例如你想在後面追加一段js,配置如下:

location / {
  sub_filter   </head> &#39;</head><script language="javascript" src="$script"></script>&#39;;
  sub_filter_once on;
}
登入後複製

以上是Nginx記錄分析響應慢的請求及替換網站回應內容怎麼配置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!