Nginx 介紹
Nginx 是一個開源、輕量級、高性能的 HTTP 和反向代理服務器,也可以用于 IMAP/POP3 代理服務器。Nginx 因其采用的異步非阻塞工作模型,使其具備高并發、低資源消耗的特性。高度模塊化設計也使得 Nginx 具備很好的擴展性,在處理靜態文件、反向代理請求等方面, Nginx 表現出了很大的優勢,同時部署維護簡單。因此絕大多數企業內部都會用到 Nginx 。
Nginx 的配置結構圖如下:
主要結構塊說明如下:
1)全局塊:配置影響 Nginx 全局的指令。
2)http塊:Nginx 配置文件中的主要上下文之一,用于定義全局的 HTTP 配置。它可以包含其他模塊的配置指令,如 server 和 upstream。
3)server塊:是 Nginx 配置文件中的另一個上下文,用于定義虛擬主機的配置。每個 server 塊代表一個虛擬主機,可以包含 listen、server_name、location 和 location 塊等指令。
4)location塊:location 是 server 上下文中的一個指令,用于定義請求的 URI 或名稱空間的匹配和處理規則。它可以包含處理請求的指令,如 proxy_pass、root、index 等。
5)upstream:upstream 用于定義一個服務器組,通常用于負載均衡。它允許 Nginx 將請求分發到多個后端服務器。
通常在 Nginx 監控中,可以通過?stub_status
?模塊提供的如下7個指標來查看 Nginx 的狀態信息。
- Active connections:當前活動的客戶端連接數,包括等待中的連接
- accepts:接受的客戶端連接總數
- handled:處理的連接總數。通常情況下,此參數的值與accepts相同,除非已經達到了某些資源限制(例如,worker_connections限制)
- equests:客戶端請求的總數
- Reading:當前Nginx正在讀取請求頭的連接數量
- Writing:當前Nginx正在將響應寫回客戶端的連接數量
但是,這些信息對于監控 Nginx 整體運行情況顯然不太夠用。Nginx VTS?模塊會提供更加豐富的 Nginx 監控指標。Nginx VTS 是 Nginx virtual host traffic status module 的簡稱,是一個專門用于 Nginx 服務器的監控模塊,它的主要目的是收集和呈現關于 Nginx 運行狀態的詳細信息,可以監控 Nginx 的流量、連接數等底層數據,對分析 Nginx 的性能非常重要。
該模塊允許用戶訪問 Nginx 的虛擬主機狀態信息,包括服務器、上游服務器(upstreams)和緩存狀態。它類似于 Nginx Plus 的實時活動監控功能。例如,一個 Nginx 的 web 服務中,會包含多個 server,通常監控的流量都是服務器總的流量。如果要分享找到流量大的 server,通常的做法是通過分析日志來進行訪問量統計。但是,有了 Nginx VTS 模塊后,通過對 server zone 的統計,各個 server 的流量可以一覽無余。除了 server 外,各個 upstream 也可以分別統計,可以很方便的查看 nginx 轉發到 upstream 的流量,結合監控可以實現動態調整等。
Nginx VTS 模塊提供了內置的 HTML 頁面,以及 JSON、HTML、JSONP 和 Prometheus 格式的數據輸出,方便用于第三方監控工具進行數據采集,并通過監控儀表板進行監控數據的呈現。
Nginx VTS 模塊的安裝和配置
模塊安裝
通過如下鏈接下載 VTS 模塊,并上傳下載文件到 Nginx 服務器(或者直接在 Nginx 服務上通過 git 下載)。
GitHub - vozlt/nginx-module-vts: Nginx virtual host traffic status module
通過如下命令獲取 Nginx 當前的配置情況:
# nginx -V
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module
通過 --add-module 添加VTS模塊,并編譯安裝 Nginx 。
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --add-module=/file_path_to/nginx-module-vts
make && make install
nginx -V -- 啟動nginx服務,并檢測開啟的模塊
模塊配置
修改 nginx.conf 配置文件,添加 VTS 相關配置。重啟 Nginx 服務。
http {vhost_traffic_status_zone;server {location /status {vhost_traffic_status_display;vhost_traffic_status_display_format prometheus;}}
}備注:vhost_traffic_status_display_format 可選格式有json、jsonp、html或prometheus。
訪問 Nginx VTS 的配置地址(?http://localhost/status?)。正常情況下會出現如下信息。
觀測云
觀測云是一款專為 IT 工程師打造的全鏈路可觀測產品,它集成了基礎設施監控、應用程序性能監控和日志管理,為整個技術棧提供實時可觀察性。這款產品能夠幫助工程師全面了解端到端的用戶體驗追蹤,了解應用內函數的每一次調用,以及全面監控云時代的基礎設施。此外,觀測云還具備快速發現系統安全風險的能力,為數字化時代提供安全保障。
部署 DataKit
DataKit 是一個開源的、跨平臺的數據收集和監控工具,由觀測云開發并維護。它旨在幫助用戶收集、處理和分析各種數據源,如日志、指標和事件,以便進行有效的監控和故障排查。DataKit 支持多種數據輸入和輸出格式,可以輕松集成到現有的監控系統中。
登錄觀測云控制臺,在 集成 -> DataKit 選擇對應安裝方式,當前采用 Linux 主機部署 DataKit 。
采集器配置
我們將通過 DataKit 中的 Nginx 采集器對 Nginx 的監控指標進行采集。配置說明如下:
1、開啟 Nginx 采集器
cp /usr/local/datakit/conf.d/nginx/nginx.conf.sample /usr/local/datakit/conf.d/nginx/nginx.conf
2、編輯 nginx.conf 文件,開啟 VTS 選項并配置監控數據的訪問地址等,如下所示:
[[inputs.nginx]]## Nginx status URL.## (Default) If not use with VTS, the formula is like this: "http://localhost/basic_status".## If using with VTS, the formula is like this: "http://localhost/status/format/json".url = "http://localhost/status/format/json"use_vts = trueuse_plus_api = falseinsecure_skip_verify = falseresponse_timeout = "20s"election = false
3、重啟 DataKit 服務讓配置生效。
datakit service -R
關鍵指標
- nginx 指標集
1、標簽
Tag | Description |
---|---|
host | Host name which installed nginx |
nginx_port | Nginx server port |
nginx_server | Nginx server host |
nginx_version | Nginx version, exist when using vts |
2、指標列表
Metric | Description | Type | Unit |
---|---|---|---|
connection_accepts | The total number of accepts client connections | int | count |
connection_active | The current number of active client connections | int | count |
connection_dropped | The total number of dropped client connections | int | count |
connection_handled | The total number of handled client connections | int | count |
connection_reading | The total number of reading client connections | int | count |
connection_requests | The total number of requests client connections | int | count |
connection_waiting | The total number of waiting client connections | int | count |
connection_writing | The total number of writing client connections | int | count |
load_timestamp | Nginx process load time in milliseconds, exist when using vts | int | msec |
pid | The pid of nginx process (only for Nginx plus) | int | count |
ppid | The ppid of nginx process (only for Nginx plus) | int | count |
- nginx_server_zone 指標集
1、標簽
Tag | Description |
---|---|
host | host name which installed nginx |
nginx_port | nginx server port |
nginx_server | nginx server host |
nginx_version | nginx version |
server_zone | server zone |
2、指標列表
Metric | Description | Type | Unit |
---|---|---|---|
code_200 | The number of responses with status code 200 (only for Nginx plus) | int | count |
code_301 | The number of responses with status code 301 (only for Nginx plus) | int | count |
code_404 | The number of responses with status code 404 (only for Nginx plus) | int | count |
code_503 | The number of responses with status code 503 (only for Nginx plus) | int | count |
discarded | The number of requests being discarded (only for Nginx plus) | int | count |
processing | The number of requests being processed (only for Nginx plus) | int | count |
received | The total amount of data received from clients. | int | B |
requests | The total number of client requests received from clients. | int | count |
response_1xx | The number of responses with status codes 1xx | int | count |
response_2xx | The number of responses with status codes 2xx | int | count |
response_3xx | The number of responses with status codes 3xx | int | count |
response_4xx | The number of responses with status codes 4xx | int | count |
response_5xx | The number of responses with status codes 5xx | int | count |
responses | The total number of responses (only for Nginx plus) | int | count |
send | The total amount of data sent to clients. | int | B |
- nginx_upstream_zone 指標集
1、標簽
Tag | Description |
---|---|
host | host name which installed nginx |
nginx_port | nginx server port |
nginx_server | nginx server host |
nginx_version | nginx version |
upstream_server | upstream server |
upstream_zone | upstream zone |
2、指標列表
Metric | Description | Type | Unit |
---|---|---|---|
active | The number of active connections (only for Nginx plus) | int | count |
backup | Whether it is configured as a backup server (only for Nginx plus) | int | count |
fails | The number of failed requests (only for Nginx plus) | int | count |
received | The total number of bytes received from this server. | int | B |
request_count | The total number of client requests received from server. | int | count |
response_1xx | The number of responses with status codes 1xx | int | count |
response_2xx | The number of responses with status codes 2xx | int | count |
response_3xx | The number of responses with status codes 3xx | int | count |
response_4xx | The number of responses with status codes 4xx | int | count |
response_5xx | The number of responses with status codes 5xx | int | count |
send | The total number of bytes sent to clients. | int | B |
state | The current state of the server (only for Nginx plus) | int | count |
unavail | The number of unavailable server (only for Nginx plus) | int | count |
weight | Weights used when load balancing (only for Nginx plus) | int | count |
- nginx_cache_zone 指標集
1、標簽
Tag | Description |
---|---|
cache_zone | cache zone |
host | host name which installed nginx |
nginx_port | nginx server port |
nginx_server | nginx server host |
nginx_version | nginx version |
2、指標列表
Metric | Description | Type | Unit |
---|---|---|---|
max_size | The limit on the maximum size of the cache specified in the configuration | int | B |
received | The total number of bytes received from the cache. | int | B |
responses_bypass | The number of cache bypass | int | count |
responses_expired | The number of cache expired | int | count |
responses_hit | The number of cache hit | int | count |
responses_miss | The number of cache miss | int | count |
responses_revalidated | The number of cache revalidated | int | count |
responses_scarce | The number of cache scarce | int | count |
responses_stale | The number of cache stale | int | count |
responses_updating | The number of cache updating | int | count |
send | The total number of bytes sent from the cache. | int | B |
used_size | The current size of the cache. | int | B |
場景視圖
登錄觀測云控制臺,點擊「場景」 -「新建儀表板」,輸入 “Nginx”, 選擇 “Nginx(VTS) 監控視圖”,點擊 “確定” 即可添加視圖。
視圖主要由如下3個部分組成:
1、總覽部分:主要顯示 Nginx 服務器的總體運行情況。包括整體的請求數,連接數,收發數據量和響應錯誤數等。
2、Server 部分:主要顯示各個虛擬主機的請求數,數據收發量和對應的響應錯誤數據情況。
3、upstream 部分:主要顯示請求分發到不同后端服務的請求數,數據收發量和對應的響應錯誤數據情況。
監控器(告警)
連接斷開異常告警
斷開連接數等于 accept(接收)和 handled(處理)之間的差值。在正常情況下,斷開的連接應為零。如果每單位時間斷開連接的速率開始上升,需要尋找導致資源飽和狀態可能的因素。
請求連接數突變
請求數的劇烈變化可能會是環境中某個地方正在發生問題,雖然它并不能確切地告訴問題發生在哪里。但是,值得關注并做進一步分析。
服務錯誤率告警
服務器錯誤率等于單位時間的 5xx 錯誤數(例如 “502 Bad Gateway”)除以請求總數(包含 1xx,2xx,3xx,4xx,5xx)。如果錯誤率過高,則可能需要進行進一步調查。
總結
Nginx VTS 模塊提供了一種強大而靈活的方式來監控和分析 Nginx 的性能和流量,對于維護和優化 Nginx 服務器提供豐富的監控數據支撐。