Nginx 可观测性最佳实践
off999 2025-01-21 20:36 21 浏览 0 评论
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 下载)。
https://github.com/vozlt/nginx-module-vts
通过如下命令获取 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 = true
use_plus_api = false
insecure_skip_verify = false
response_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 服务器提供丰富的监控数据支撑。
相关推荐
- 推荐一款Python的GUI可视化工具(python 可视化工具)
-
在Python基础语法学习完成后,进一步开发应用界面时,就需要涉及到GUI了,GUI全称是图形用户界面(GraphicalUserInterface,又称图形用户接口),采用图形方式显示的计算机操...
- 教你用Python绘制谷歌浏览器的3种图标
-
前两天在浏览matplotlib官方网站时,笔者无意中看到一个挺有意思的图片,就是用matplotlib制作的火狐浏览器的logo,也就是下面这个东东(网页地址是https://matplotlib....
- 小白学Python笔记:第二章 Python安装
-
Windows操作系统的python安装:Python提供Windows、Linux/UNIX、macOS及其他操作系统的安装包版本,结合自己的使用情况,此处仅记录windows操作系统的python...
- Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字
-
Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字一、项目功能利用Tkinter组件中的Canvas绘制图形和文字。二、项目分析要在窗体中绘制图形和文字,需先导入Tkinter组...
- 一文吃透Python虚拟环境(python虚拟环境安装和配置)
-
摘要在Python开发中,虚拟环境是一种重要的工具,用于隔离不同项目的依赖关系和环境配置。本文将基于windows平台介绍四种常用的Python虚拟环境创建工具:venv、virtualenv、pip...
- 小白也可以玩的Python爬虫库,收藏一下
-
最近,微软开源了一个项目叫「playwright-python」,作为一个兴起项目,出现后受到了大家热烈的欢迎,那它到底是什么样的存在呢?今天为你介绍一下这个传说中的小白神器。Playwright是...
- python环境安装+配置教程(python安装后怎么配置环境变量)
-
安装python双击以下软件:弹出一下窗口需选择一些特定的选项默认选项不需要更改,点击next勾选以上选项,点击install进度条安装完毕即可。到以下界面,证明安装成功。接下来安装库文件返回电脑桌面...
- colorama,一个超好用的 Python 库!
-
大家好,今天为大家分享一个超好用的Python库-colorama。Github地址:https://github.com/tartley/coloramaPythoncolorama库是一...
- python制作仪表盘图(python绘制仪表盘)
-
今天教大家用pyecharts画仪表盘仪表盘(Gauge)是一种拟物化的图表,刻度表示度量,指针表示维度,指针角度表示数值。仪表盘图表就像汽车的速度表一样,有一个圆形的表盘及相应的刻度,有一个指针...
- 总结90条写Python程序的建议(python写作)
-
1.首先 建议1、理解Pythonic概念—-详见Python中的《Python之禅》 建议2、编写Pythonic代码 (1)避免不规范代码,比如只用大小写区分变量、使用容易...
- [oeasy]python0137_相加运算_python之禅_import_this_显式转化
-
变量类型相加运算回忆上次内容上次讲了是从键盘输入变量input函数可以有提示字符串需要有具体的变量接收输入的字符串输入单个变量没有问题但是输入两个变量之后一相加就非常离谱添加图片注释,不超过1...
- Python入门学习记录之一:变量(python中变量的规则)
-
写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...
- 掌握Python的"魔法":特殊方法与属性完全指南
-
在Python的世界里,以双下划线开头和结尾的"魔法成员"(如__init__、__str__)是面向对象编程的核心。它们赋予开发者定制类行为的超能力,让自定义对象像内置类型一样优雅工...
- 11个Python技巧 不Pythonic 实用大于纯粹
-
虽然Python有一套强大的设计哲学(体现在“Python之禅”中),但总有一些情况需要我们“打破规则”来解决特定问题。这触及了Python哲学中一个非常核心的理念:“实用主义胜于纯粹主义”...
- Python 从入门到精通 第三课 诗意的Python之禅
-
导言:Python之禅,英文名是TheZenOfPython。最早由TimPeters在Python邮件列表中发表,它包含了影响Python编程语言设计的20条软件编写原则。它作为复活节彩蛋...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)