Nginx虛擬主機流量狀態模塊(nginx-module-vts)使用說明文檔(四)


裝完NG,為了拿到各種狀態指標,就要對NG做監控。
Github 2.3k的開源項目nginx-module-vts沒准真是你需求的。
鏈接數,qps,1xx、2xx,、3xx、4xx、5xx的響應數,響應耗時,響應時間分布,訪問用戶國家分布;甚至是基於各種狀態的流量控制統統能滿足你的需求。
nginx-module-vts具體怎么用?我們還是從官方文檔開始擼吧,還是英文的,那就翻譯一下吧。
內容有點長,你看到的文檔會四篇文檔的方式分別出現。這里是第四篇,第一篇你可以點這里查看第二篇你可以點這里查看第三篇你可以點這里查看

Nginx虛擬主機流量狀態模塊

目錄

16、指令

draw_io_vts_diagram

vhost_traffic_status

- -
語法 vhost_traffic_status <on|off>
默認值 off
語境 http, server, location

說明: 啟用或禁用模塊工作。如果設置vhost_traffic_status_zone指令,則會自動啟用。

vhost_traffic_status_zone

- -
語法 vhost_traffic_status_zone [shared:name:size]
默認值 shared:vhost_traffic_status:1m
語境 http

說明: 為共享內存區域設置參數,該區域將保存各種鍵的狀態。緩存在所有工作進程之間共享。
在大多數情況下,nginx-module-vts使用的共享內存大小不會增加太多。
當使用vhost_traffic_status_filter_by_set_key指令時,共享內存的大小會增加很多,但是如果過濾器的鍵是固定的(例如,國家代碼的總數約為240),則不會持續增加。

如果使用vhost_traffic_status_filter_by_set_key指令,請按如下方式設置:

  • 默認設置為32M共享內存大小。(vhost_traffic_status_zone shared:vhost_traffic_status:32m)
  • 如果出現("ngx_slab_alloc() failed: no memory in vhost_traffic_status_zone")的異常日志,則增加到大於(usedSize*2)

vhost_traffic_status_dump

- -
語法 vhost_traffic_status_dump path [period]
默認值 -
語境 http

說明: 啟用統計數據存儲和還原。
path 是存儲統計數據的位置。(例如 /var/log/nginx/vts.db)
period 是一個備份周期。(默認: 60s)
如果nginx被(SIGKILL)信號退出,則不管備份周期如何,它都會立即備份。

vhost_traffic_status_display

- -
語法 vhost_traffic_status_display
默認值 -
語境 http, server, location

說明: 啟用或禁用顯示處理程序模塊。

vhost_traffic_status_display_format

- -
語法 vhost_traffic_status_display_format <json|html|jsonp|prometheus>
默認值 json
語境 http, server, location

說明: 設置顯示處理模塊輸出的格式。
如果設置json,則將用JSON文檔響應。
如果設置了html,則將以內置的HTML格式的實時儀表板進行響應。
如果設置jsonp,則將使用JSONP回調函數(默認值:ngx_http_vhost_traffic_status_JSONP_callback)響應。
如果設置prometheus,則將用一個prometheus 文檔來響應。

vhost_traffic_status_display_jsonp

- -
語法 vhost_traffic_status_display_jsonp callback
默認值 ngx_http_vhost_traffic_status_jsonp_callback
語境 http, server, location

說明: 設置JSONP的回調名稱。

vhost_traffic_status_display_sum_key

- -
語法 vhost_traffic_status_display_sum_key name
默認值 *
語境 http, server, location

說明: 在serverZones字段的JSON中設置sum鍵對應的字符串。默認的sum鍵字符串是“*”。

vhost_traffic_status_filter

- -
語法 vhost_traffic_status_filter <on|off>
默認值 on
語境 http, server, location

說明: 啟用或禁用篩選器功能。

vhost_traffic_status_filter_by_host

- -
語法 vhost_traffic_status_filter_by_host <on|off>
默認值 off
語境 http, server, location

說明: 啟用或禁用keys by Host header 字段。
如果設置on同時nginx的server_name指令設置多個或以星號開頭的通配符名稱,例如“.example.org”,同時請求server使用主機名,如(a|b|c).example.org或.example.org,則json格式的 serverZones將按如下方式打印:

server {
  server_name *.example.org;
  vhost_traffic_status_filter_by_host on;

  ...

}
  ...
  "serverZones": {
      "a.example.org": {
      ...
      },
      "b.example.org": {
      ...
      },
      "c.example.org": {
      ...
      }
      ...
   },
   ...

It provides the same function that set vhost_traffic_status_filter_by_set_key $host.
它與設置vhost_traffic_status_filter_by_set_key $host提供了相同的函數。

vhost_traffic_status_filter_by_set_key

- -
語法 vhost_traffic_status_filter_by_set_key key [name]
默認值 -
語境 http, server, location

說明: 啟用由用戶定義的鍵。
key 是用於計算流量的鍵字符串。
name 是用於計算流量的分組字符串。
keyname 可以包含變量,如$host,$server_name。
指定第二個參數name時對應的分組信息在filterZones中體現。
如果沒有指定第二個參數name,則key的組屬於serverZones
geoip模塊示例如下:

server {
  server_name example.org;
  vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;

  ...

}
  ...
  "serverZones": {
  ...
  },
  "filterZones": {
      "country::example.org": {
          "KR": {
              "requestCounter":...,
              "inBytes":...,
              "outBytes":...,
              "responses":{
                  "1xx":...,
                  "2xx":...,
                  "3xx":...,
                  "4xx":...,
                  "5xx":...,
                  "miss":...,
                  "bypass":...,
                  "expired":...,
                  "stale":...,
                  "updating":...,
                  "revalidated":...,
                  "hit":...,
                  "scarce":...
              },
              "requestMsecCounter":...,
              "requestMsec":...,
              "requestMsecs":{
                  "times":[...],
                  "msecs":[...]
              },
          },
          "US": {
          ...
          },
          ...
      },
      ...
  },
  ...

vhost_traffic_status_filter_check_duplicate

- -
語法 vhost_traffic_status_filter_check_duplicate <on|off>
默認值 on
語境 http, server, location

說明: 啟用或禁用vhost_traffic_status_filter_by_set_key的重復數據刪除。
如果啟用此選項,則在每個指令(http、server、location)中只處理一個重復值(key + name)。

vhost_traffic_status_filter_max_node

- -
語法 vhost_traffic_status_filter_max_node number [string ...]
默認值 0
語境 http

說明: 基於指定的numberstring參數啟用篩選器大小限制。
如果超過制定的number,則通過LRU 算法刪除現有節點。
number參數是將限制的節點的大小。默認值0不限制過濾器。
一個節點是JSON文檔中filterZones中的一個對象。
string參數匹配在vhost_traffic_status_filter_by_set_key指令中設置的組字符串對應的值。
即使只有第一部分匹配,匹配也是成功的,就像正則表達式/^string.*/
默認情況下,如果不設置string參數,則它將應用於所有的篩選器。

例如:

$ vi nginx.conf

http {

    geoip_country /usr/share/GeoIP/GeoIP.dat;

    vhost_traffic_status_zone;

    # The all filters are limited to a total of 16 nodes.
    # vhost_traffic_status_filter_max_node 16

    # The `/^uris.*/` and `/^client::ports.*/` group string patterns are limited to a total of 64 nodes.
    vhost_traffic_status_filter_max_node 16 uris client::ports

    ...

    server {

        server_name example.org;

        ...

        vhost_traffic_status_filter_by_set_key $uri uris::$server_name;
        vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name;
        vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;

    }
}

$ for i in {0..1000}; do curl -H 'Host: example.org' -i "http://localhost:80/test$i"; done

screenshot-vts-filter-max-node

在上面的例子中,/^uris.*//^client::ports.*/分組字符串被限制為總共16個節點。其他過濾器,如country::.*不受限制。

vhost_traffic_status_limit

- -
語法 vhost_traffic_status_limit <on|off>
默認值 on
語境 http, server, location

說明: 啟用或禁用限制功能。

vhost_traffic_status_limit_traffic

- -
語法 vhost_traffic_status_limit_traffic member:size [code]
默認值 -
語境 http, server, location

說明: 為指定member啟用流量限制。
參數member是限制流量的統計粒度字符串。
參數size是限制流量的大小字符串,單位(k/m/g)。
參數code是請求被拒絕時候的響應代碼。(默認響應碼:503)

可用的member字符串如下

  • request
    • 從客戶端接收的請求總數。
  • in
    • 從客戶端接收的總字節數。
  • out
    • 發送到客戶端的總字節數。
  • 1xx
    • 狀態代碼為1xx的響應數。
  • 2xx
    • 狀態代碼為2xx的響應數。
  • 3xx
    • 狀態代碼為3xx的響應數。
  • 4xx
    • 狀態代碼為4xx的響應數。
  • 5xx
    • 狀態代碼為5xx的響應數。
  • cache_miss
    • 緩存未命中數。
  • cache_bypass
    • 緩存旁路數。
  • cache_expired
    • 緩存已過期數。
  • cache_stale
    • 緩存失效的數量。
  • cache_updating
    • 緩存更新的次數。
  • cache_revalidated
    • 重新驗證的緩存數。
  • cache_hit
    • 緩存命中數
  • cache_scarce
    • 未達緩存要求的請求的數量。

vhost_traffic_status_limit_traffic_by_set_key

- -
語法 vhost_traffic_status_limit_traffic_by_set_key key member:size [code]
默認值 -
語境 http, server, location

說明: 針對制定的keymember進行流量控制
參數key是限制流量的key字符串
參數member是限制流量的member字符串
參數size是流量的限制的大小字符串,單位(k/m/g)。
參數code是請求被拒絕時候的響應代碼。(默認響應碼:503)

key 語法如下:

  • group@[subgroup@]name

可用的group 字符串如下:

  • NO
    • server group。
  • UA
    • upstream alone group。
  • UG
    • The group of upstream group.(use subgroup)
  • CC
    • cache group。
  • FG
    • filter group。(use subgroup)

可用的member字符串如下:

  • request
    • 從客戶端接收的請求總數。
  • in
    • 從客戶端接收的總字節數。
  • out
    • 發送到客戶端的總字節數。
  • 1xx
    • 狀態代碼為1xx的響應數。
  • 2xx
    • 狀態代碼為2xx的響應數。
  • 3xx
    • 狀態代碼為3xx的響應數。
  • 4xx
    • 狀態代碼為4xx的響應數。
  • 5xx
    • 狀態代碼為5xx的響應數。
  • cache_miss
    • 緩存未命中數。
  • cache_bypass
    • 緩存旁路數。
  • cache_expired
    • 緩存已過期數。
  • cache_stale
    • 緩存失效的數量。
  • cache_updating
    • 緩存更新的次數。
  • cache_revalidated
    • 重新驗證的緩存數。
  • cache_hit
    • 緩存命中數
  • cache_scarce
    • 未達緩存要求的請求的數量。

這里的membervhost_traffic_status_limit_traffic指令下的member相同。

vhost_traffic_status_limit_check_duplicate

- -
語法 vhost_traffic_status_limit_check_duplicate <on|off>
默認值 on
語境 http, server, location

說明: 啟用或禁用vhost_traffic_status_limit_by_set_key的重復數據刪除。
如果啟用此選項,則只處理每個指令(http、server、location)中的重復值(member或者key + member)中的一個。

vhost_traffic_status_set_by_filter

- -
語法 vhost_traffic_status_set_by_filter $variable group/zone/name
默認值 -
語境 http, server, location, if

說明: 獲取存儲在共享內存中的指定狀態值。
它可以獲取幾乎所有的狀態值,並將獲得的值存儲在第一個參數$variable中。

  • group
    • server
    • filter
    • upstream@alone
    • upstream@group
    • cache
  • zone
    • server
      • name
    • filter
      • filter_group@name
    • upstream@group
      • upstream_group@name
    • upstream@alone
      • @name
    • cache
      • name
  • name
    • requestCounter
      • 從客戶端接收的客戶端請求總數。
    • requestMsecCounter
      • 累積的請求處理時間(毫秒)。
    • requestMsec
      • 請求處理時間的平均值(毫秒)
    • responseMsecCounter
      • 僅上游響應處理時間的累積數(毫秒)。
    • responseMsec
      • 僅上游響應處理時間的平均值(毫秒)。
    • inBytes
      • 從客戶端接收的總字節數。
    • outBytes
      • 發送到客戶端的總字節數
    • 1xx, 2xx, 3xx, 4xx, 5xx
      • 狀態代碼為1xx、2xx、3xx、4xx和5xx的響應數。
    • cacheMaxSize
      • 配置中指定的緩存的最大大小限制。
    • cacheUsedSize
      • 緩存的當前大小。
    • cacheMiss
      • 緩存未命中數。
    • cacheBypass
      • 緩存旁路數。
    • cacheExpired
      • 緩存過期數。
    • cacheStale
      • 緩存失效的數量。
    • cacheUpdating
      • 緩存更新的次數。
    • cacheRevalidated
      • 重新驗證的緩存數。
    • cacheHit
      • 緩存命中的數量
    • cacheScarce
      • 未達緩存要求的請求的數量。
    • weight
      • server的當前權重值。
    • maxFails
      • server的當前max_fails設置。
    • failTimeout
      • server的當前fail_timeout設置。
    • backup
      • server的當前備份設置(0或1)
    • down
      • server的當前下線設置(0或1)

注意:name區分大小寫。所有返回值都采用整數類型。

例如:

  • serverZones中的請求計數器設置
    • vhost_traffic_status_set_by_filter $requestCounter server/example.org/requestCounter
  • filterZones中的請求計數器設置
    • vhost_traffic_status_set_by_filter $requestCounter filter/country::example.org@KR/requestCounter
  • upstreamZones中的請求計數器設置
    • vhost_traffic_status_set_by_filter $requestCounter upstream@group/backend@10.10.10.11:80/requestCounter
  • upstreamZones::nogroups中的請求計數器設置
    • vhost_traffic_status_set_by_filter $requestCounter upstream@alone/10.10.10.11:80/requestCounter
  • cacheZones中的命中率計數器設置
  • vhost_traffic_status_set_by_filter $cacheHit cache/my_cache_name/cacheHit

vhost_traffic_status_average_method

- -
語法 vhost_traffic_status_average_method <AMM或WMA> [period]
默認值 AMM 60s
語境 http, server, location

說明: 設置計算響應處理時間平均值的公式和方法。
The period is an effective time of the values used for the average calculation.(Default: 60s)
參數period用來平均值的有效期(默認值: 60s)。
如果period設置為0,則忽略有效時間。在這種情況下,即使沒有請求並且經過一段時間后,也會顯示最后的平均值。
在JSON中,對應的值是requestMsecresponseMsec

vhost_traffic_status_histogram_buckets

- -
語法 vhost_traffic_status_histogram_buckets second ...
默認值 -
語境 http, server, location

說明: 設置要在直方圖中使用的觀察桶。
默認情況下,如果不設置此指令,它將不起作用。
參數second可以用小數位表示,最小值為0.001(1ms)。
桶的最大尺寸為32。如果這個值不夠,請在src/ngx_http_vhost_traffic_status_node.h中更改NGX_HTTP_VHOST_TRAFFIC_STATUS_DEFAULT_BUCKET_LEN對應的值。

例如:

  • vhost_traffic_status_histogram_buckets 0.005 0.01 0.05 0.1 0.5 1 5 10
    • 觀察桶是[5ms 10ms 50ms 1s 5s 10s]。
  • vhost_traffic_status_histogram_buckets 0.005 0.01 0.05 0.1
    • 觀察桶是[5ms 10ms 50ms 1s]。

注意: 默認情況下,如果不設置此指令,則直方圖統計不起作用。
使用指令vhost_traffic_status_dump恢復的直方圖不受指令vhost_traffic_status_histogram_buckets對存儲桶的更改的影響。
因此,在通過vhost_traffic_status_histogram_buckets指令更改存儲桶之前,必須首先刪除zone或dump文件。
與上面類似,在第一次使用直方圖時也需要刪除dump文件。

vhost_traffic_status_bypass_limit

- -
語法 vhost_traffic_status_bypass_limit <on|off>
默認值 off
語境 http, server, location

說明: 啟用或禁用繞過vhost_traffic_status_limit指令。
如果啟用此選項,將繞過限制功能。如果通過/status訪問狀態網頁,而希望不考慮vhost_traffic_status_limit指令對它的限制,則這一點非常有用,使用如下的配置:

http {
    vhost_traffic_status_zone;

    ...

    server {

        ...

        location /status {
            vhost_traffic_status_bypass_limit on;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

vhost_traffic_status_bypass_stats

- -
語法 vhost_traffic_status_bypass_stats <on|off>
默認值 off
語境 http, server, location

說明: 啟用或禁用繞過vhost_traffic_status。
如果啟用此選項,將繞過流量狀態統計功能。也就是說,它被排除在流量狀態統計之外。
如果需要在像/status這樣的狀態網頁中忽略請求,可以使用如下的設置:

http {
    vhost_traffic_status_zone;

    ...

    server {

        ...

        location /status {
            vhost_traffic_status_bypass_stats on;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

17、另請參閱

18、待辦事項

19、捐贈

License

20、作者

YoungJoo.Kim(김영주) [vozltx@gmail.com]

凋謝的不是花,萎蔫的是時間;星辰大海,點滴為途。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM