Nginx 可觀測性最佳實踐

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、標簽

TagDescription
hostHost name which installed nginx
nginx_portNginx server port
nginx_serverNginx server host
nginx_versionNginx version, exist when using vts

2、指標列表

MetricDescriptionTypeUnit
connection_acceptsThe total number of accepts client connectionsintcount
connection_activeThe current number of active client connectionsintcount
connection_droppedThe total number of dropped client connectionsintcount
connection_handledThe total number of handled client connectionsintcount
connection_readingThe total number of reading client connectionsintcount
connection_requestsThe total number of requests client connectionsintcount
connection_waitingThe total number of waiting client connectionsintcount
connection_writingThe total number of writing client connectionsintcount
load_timestampNginx process load time in milliseconds, exist when using vtsintmsec
pidThe pid of nginx process (only for Nginx plus)intcount
ppidThe ppid of nginx process (only for Nginx plus)intcount
  • nginx_server_zone 指標集

1、標簽

TagDescription
hosthost name which installed nginx
nginx_portnginx server port
nginx_servernginx server host
nginx_versionnginx version
server_zoneserver zone

2、指標列表

MetricDescriptionTypeUnit
code_200The number of responses with status code 200 (only for Nginx plus)intcount
code_301The number of responses with status code 301 (only for Nginx plus)intcount
code_404The number of responses with status code 404 (only for Nginx plus)intcount
code_503The number of responses with status code 503 (only for Nginx plus)intcount
discardedThe number of requests being discarded (only for Nginx plus)intcount
processingThe number of requests being processed (only for Nginx plus)intcount
receivedThe total amount of data received from clients.intB
requestsThe total number of client requests received from clients.intcount
response_1xxThe number of responses with status codes 1xxintcount
response_2xxThe number of responses with status codes 2xxintcount
response_3xxThe number of responses with status codes 3xxintcount
response_4xxThe number of responses with status codes 4xxintcount
response_5xxThe number of responses with status codes 5xxintcount
responsesThe total number of responses (only for Nginx plus)intcount
sendThe total amount of data sent to clients.intB
  • nginx_upstream_zone 指標集

1、標簽

TagDescription
hosthost name which installed nginx
nginx_portnginx server port
nginx_servernginx server host
nginx_versionnginx version
upstream_serverupstream server
upstream_zoneupstream zone

2、指標列表

MetricDescriptionTypeUnit
activeThe number of active connections (only for Nginx plus)intcount
backupWhether it is configured as a backup server (only for Nginx plus)intcount
failsThe number of failed requests (only for Nginx plus)intcount
receivedThe total number of bytes received from this server.intB
request_countThe total number of client requests received from server.intcount
response_1xxThe number of responses with status codes 1xxintcount
response_2xxThe number of responses with status codes 2xxintcount
response_3xxThe number of responses with status codes 3xxintcount
response_4xxThe number of responses with status codes 4xxintcount
response_5xxThe number of responses with status codes 5xxintcount
sendThe total number of bytes sent to clients.intB
stateThe current state of the server (only for Nginx plus)intcount
unavailThe number of unavailable server (only for Nginx plus)intcount
weightWeights used when load balancing (only for Nginx plus)intcount
  • nginx_cache_zone 指標集

1、標簽

TagDescription
cache_zonecache zone
hosthost name which installed nginx
nginx_portnginx server port
nginx_servernginx server host
nginx_versionnginx version

2、指標列表

MetricDescriptionTypeUnit
max_sizeThe limit on the maximum size of the cache specified in the configurationintB
receivedThe total number of bytes received from the cache.intB
responses_bypassThe number of cache bypassintcount
responses_expiredThe number of cache expiredintcount
responses_hitThe number of cache hitintcount
responses_missThe number of cache missintcount
responses_revalidatedThe number of cache revalidatedintcount
responses_scarceThe number of cache scarceintcount
responses_staleThe number of cache staleintcount
responses_updatingThe number of cache updatingintcount
sendThe total number of bytes sent from the cache.intB
used_sizeThe current size of the cache.intB

場景視圖

登錄觀測云控制臺,點擊「場景」 -「新建儀表板」,輸入 “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 服務器提供豐富的監控數據支撐。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/66166.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/66166.shtml
英文地址,請注明出處:http://en.pswp.cn/web/66166.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《汽車維護與修理》是什么級別的期刊?是正規期刊嗎?能評職稱嗎?

?問題解答: 問:《汽車維護與修理》是不是核心期刊? 答:不是,是知網收錄的正規學術期刊。 問:《汽車維護與修理》級別? 答:國家級。主管單位:中國汽車維修行業協會 …

PHP智慧小區物業管理小程序

🌟智慧小區物業管理小程序:重塑社區生活,開啟便捷高效新篇章 🌟 智慧小區物業管理小程序是一款基于PHPUniApp精心雕琢的智慧小區物業管理小程序,它猶如一股清新的科技之風,吹進了現代智慧小區的每一個角落…

洛谷P4868 Preprefix sum

洛谷傳送門 題目描述 前綴和(prefix sum)𝑆𝑖。 前前綴和(preprefix sum)則把 𝑆𝑖 作為原序列再進行前綴和。記再次求得前綴和第 𝑖 個是 𝑆&#x1d446…

機器學習中的凸函數和梯度下降法

一、凸函數 在機器學習中,凸函數 和 凸優化 是優化問題中的重要概念,許多機器學習算法的目標是優化一個凸函數。這些概念的核心思想圍繞著優化問題的簡化和求解效率。下面從簡單直觀的角度來解釋。 1. 什么是凸函數? 數學定義 一個函數 f…

vue3使用vue-native-websocket-vue3通訊

vue3使用vue-native-websocket-vue3通訊 插件使用一、啟用Vuex集成1.在mian.js中2.store/index.js文件中3.要websocket使用的頁面 二、啟用Piain集成1.在mian.js中2.根目錄下創建store文件夾,分別創建PiniaType.ts,store.ts,useSocketStore.t…

Windows圖形界面(GUI)-QT-C/C++ - Qt控件與布局系統詳解

公開視頻 -> 鏈接點擊跳轉公開課程博客首頁 -> ???鏈接點擊跳轉博客主頁 目錄 Qt布局系統(Layouts) 布局管理器基礎 高級布局技巧 嵌套布局 設置間距和邊距 常用控件詳解 按鈕類控件 QPushButton (標準按鈕) QRadioButton (單選按鈕) QCheckBox (復選框) …

深入理解 ECMAScript 2024 新特性:字符串 isWellFormed 方法

ECMAScript 2024 引入了一個新的字符串實例方法:String.prototype.isWellFormed。這一新增功能是為了幫助開發者更容易地驗證字符串是否為有效的 Unicode 文本。本文將詳細介紹這一方法的使用場景、實現原理及其在實際應用中的價值。 String.prototype.isWellFormed…

[Linux]Docker快速上手操作教程

前言 以下命令并不是docker的所有,僅涉及日常使用時最最常用的命令。 目的之一時給入門的朋友熟悉學習,其二時我自己偶爾使用時備忘。 一、概念 簡單介紹下docker的相關概念: 鏡像:Docker 鏡像是一個輕量級、可執行的獨立軟件…

【算法學習筆記】32:篩法求解歐拉函數

上節學習的是求一個數 n n n的歐拉函數,因為用的試除法,所以時間復雜度是 O ( n ) O(\sqrt{n}) O(n ?),如果要求 m m m個數的歐拉函數,那么就會花 O ( m n ) O(m \sqrt{n}) O(mn ?)的時間。如果是求連續一批數的歐拉函數&#x…

生產管理看板助力節能科技公司實現數據自動化管理

在節能科技公司的生產過程中,數據管理的自動化是提高生產效率和產品質量的關鍵。然而,許多公司在數據記錄、展示、對比和存檔方面仍面臨諸多痛點,如產品檢測數據無法自動記錄、缺乏直觀的產線狀態展示、檢測數據對比繁瑣耗時,以及…

leetcode 115. 不同的子序列

題目:115. 不同的子序列 - 力扣(LeetCode) 動態規劃問題,f[i][j]表示s的第i個元素匹配到t的第j個元素,有多少種結果 f[i][j] f[i - 1][j] (s[i] t[j] ? f[i - 1][j - 1] : 0) 答案就是 f[s.length() - 1][t.len…

【C++】B2112 石頭剪子布

博客主頁: [小????????] 本文專欄: C 文章目錄 💯前言💯題目描述游戲規則:輸入格式:輸出格式:輸入輸出樣例:解題分析與實現 💯我的做法實現邏輯優點與不足 &#x1f4af…

內存快照:宕機后Redis如何實現快速恢復?

文章目錄 給哪些內存數據做快照?快照時數據能修改嗎?可以每秒做一次快照嗎?小結每課一問 更多redis相關知識 上節課,我們學習了 Redis 避免數據丟失的 AOF 方法。這個方法的好處,是每次執行只需要記錄操作命令,需要持…

系統架構設計師考點—項目管理

一、備考指南 項目管理主要考查的是進度管理、軟件配置管理、質量管理、風險管理等相關知識,近幾年都沒有考查過,但是有可能在案例分析中考查關鍵路徑的技術問題,考生了解為主。 二、重點考點 1、項目的十大管理(速記&#xff1…

iOS - Objective-C 底層實現中的哈希表

1. 關聯對象存儲&#xff08;AssociationsHashMap&#xff09; // 關聯對象的哈希表實現 typedef DenseMap<const void *, ObjcAssociation> ObjectAssociationMap; typedef DenseMap<DisguisedPtr<objc_object>, ObjectAssociationMap> AssociationsHashMa…

兩分鐘解決 :![rejected] master -> master (fetch first) , 無法正常push到遠端庫

目錄 分析問題的原因解決 分析問題的原因 在git push的時候莫名遇到這種情況 若你在git上修改了如README.md的文件。由于本地是沒有README.md文件的&#xff0c;所以導致 遠端倉庫git和本地不同步。 將遠端、本地進行合并就可以很好的解決這個問題 注意&#xff1a;直接git pu…

Ubuntu Server 24.04 配置靜態IP

Ubuntu Server 24.04 配置靜態IP 提示&#xff1a;基于Ubuntu Server 24.04進行配置 文章目錄 Ubuntu Server 24.04 配置靜態IP一、查看網卡信息二、修改網卡信息三、使網卡配置生效四、測試 一、查看網卡信息 使用命令 ip a lo 為本地回環地址 ens33 真實網卡地址 shanfengubu…

微服務之松耦合

參考&#xff1a;https://microservices.io/post/architecture/2023/03/28/microservice-architecture-essentials-loose-coupling.html There’s actually two different types of coupling: runtime coupling - influences availability design-time coupling - influences…

Django 和 Vue3 前后端分離開發筆記

Django 和 Vue3 前后端分離開發筆記 1. Django Ninja API Django Ninja 是一個用于使用 Django 和 Python 3.6 類型提示構建 API 的網絡框架。它具有以下主要特點&#xff1a; 簡單易懂&#xff1a;設計為易于使用和符合直覺&#xff0c;適合快速上手。快速執行&#xff1a;…

44_Lua迭代器

在Lua中,迭代器是一種用于遍歷集合元素的重要工具。掌握迭代器的使用方法,對于提高Lua編程的效率和代碼的可讀性具有重要意義。 1.迭代器概述 1.1 迭代器介紹 迭代器是一種設計模式,它提供了一種訪問集合元素的方法,而不需要暴露其底層結構。在Lua中,迭代器通常以一個函…