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、标签
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 服务器提供丰富的监控数据支撑。
相关文章:

Nginx 可观测性最佳实践
Nginx 介绍 Nginx 是一个开源、轻量级、高性能的 HTTP 和反向代理服务器,也可以用于 IMAP/POP3 代理服务器。Nginx 因其采用的异步非阻塞工作模型,使其具备高并发、低资源消耗的特性。高度模块化设计也使得 Nginx 具备很好的扩展性,在处理静…...

LabVIEW光流跟踪算法
1. 光流跟踪算法的概述 光流(Optical Flow)是一种图像处理技术,用于估算图像中像素点的运动。通过比较连续帧图像,光流算法可以分析图像中的运动信息,广泛用于目标跟踪、运动检测和视频处理等场景。该示例使用了NI Vi…...
Jira用例自动去除summary重复用例
title: Jira用例自动去除summary重复用例 tags: - jira - python categories: - python一、背景与需求二、解决方案思路三、实施步骤本文永久更新地址: 在使用 Jira 进行项目管理时,测试用例的维护至关重要。随着项目推进,用例数量增多,可能…...
基于openEuler22.03SP4部署Prometheus+Grafana
测试环境 Virtual Box,openEuler-22.03-LTS-SP4-x86_64-dvd.iso,4 vCPU, 8G RAM, 60 vDisk。最小化安装。需联网。 系统环境 关闭防火墙 systemctl stop firewalld systemctl disable firewalld systemctl status firewalld selinux关闭 sed -ri…...

泛目录和泛站有什么差别
啥是 SEO 泛目录? 咱先来说说 SEO 泛目录是啥。想象一下,你有一个巨大的图书馆,里面的书架上摆满了各种各样的书,每一本书都代表着一个网页。而 SEO 泛目录呢,就像是一个超级图书管理员,它的任务就是把这些…...
css 布局及动画应用(flex+transform+transition+animation)
文章目录 css 布局及动画应用animationtransform,transition,animation 综合应用实例代码实例解释 css 布局及动画应用 Display用法 作用:用于控制元素的显示类型,如块级元素、内联元素、无显示等。常见属性值及示例:…...

springboot vue uniapp 仿小红书 1:1 还原 (含源码演示)
线上预览: 移动端 http://8.146.211.120:8081/ 管理端 http://8.146.211.120:8088/ 小红书凭借优秀的产品体验 和超高人气 目前成为笔记类产品佼佼者 此项目将详细介绍如何使用Vue.js和Spring Boot 集合uniapp 开发一个仿小红书应用,凭借uniapp 可以在h5 小程序 app…...
lombok在高版本idea中注解不生效的解决
环境: IntelliJ IDEA 2024.3.1.1 Spring Boot Maven 问题描述 使用AllArgsConstructor注解一个用户类,然后调用全参构造方法创建对象,出现错误: java: 无法将类 com.itheima.pojo.User中的构造器 User应用到给定类型; 需要:…...

跨境电商领域云手机之选:亚矩阵云手机的卓越优势
在跨境电商蓬勃发展的当下,云手机已成为众多企业拓展海外市场的得力助手。亚矩阵云手机凭借其独特优势,在竞争激烈的云手机市场中崭露头角。不过,鉴于市场上云手机服务供应商繁多,企业在抉择时需对诸多要素予以审慎考量。 跨境电商…...

Linux第二课:LinuxC高级 学习记录day02
2.4、shell中的特殊字符 2.4.4、命令置换符 或者 $() 反引号:esc下面的按键,英文状态下直接按 功能:将一个命令的输出作为另一个命令的参数 echo 不会认为hostname是一个命令 加上 之后,先执行hostname,拿到主机名…...
6. NLP自然语言处理(Natural Language Processing)
自然语言是指人类日常使用的语言,如中文、英语、法语等。 自然语言处理是人工智能(AI)领域中的一个重要分支,它结合了计算机科学、语言学和统计学的方法,通过算法对文本和语音进行分析,使计算机能够理解、解…...

win10电脑 定时关机
win10电脑 定时关机 https://weibo.com/ttarticle/p/show?id2309405110707766296723 二、使用任务计划程序设置定时关机打开任务计划程序: 按下“Win S”组合键,打开搜索框。 在搜索框中输入“任务计划程序”,然后点击搜索结果中的“任务…...
linux删除用户
1、查看账号 cat /etc/passwd 查看所有用户账号信息:该文件记录了系统中的所有用户账号信息,包括用户名、用户ID、用户所属组等。 2、删除账号 基本删除:使用userdel命令删除用户账号,格式为userdel [选项] 用户名。如果不加任…...

FPGA的 基本结构(Xilinx 公司Virtex-II 系列FPGA )
以Xilinx 公司Virtex-II 系列FPGA 为例,其基本结构由下图所示。它是主要由两大部分组成:可编程输入/输出(Programmable I/Os)部分和内部可配置(Configurable Logic)部分。 可编程输入/输出(I/Os…...
Springboot项目如何消费Kafka数据
目录 一、引入依赖二、添加Kafka配置三、创建 Kafka 消费者(一)Kafka生产的消息是JSON 字符串1、方式一2、方式二:需要直接访问消息元数据 (二)Kafka生产的消息是对象Order 四、创建 启动类五、配置 Kafka 生产者&…...
LeetCode 热题 100 | 子串
子串基础 前缀和:前面的数加在一起等于多少,放进map里,key为和,value为这个和出现的次数。单调队列:单调递增/递减队列,每次加入新元素,比新元素大/小的元素全部弹出。滑动窗口:两层…...

深度学习笔记11-优化器对比实验(Tensorflow)
🍨 本文为🔗365天深度学习训练营中的学习记录博客🍖 原作者:K同学啊 目录 一、导入数据并检查 二、配置数据集 三、数据可视化 四、构建模型 五、训练模型 六、模型对比评估 七、总结 一、导入数据并检查 import pathlib,…...
【掌握 JavaScript 数组迭代:map 和 includes 的使用技巧】
map map()方法是数组原型的一个函数,用于对数组的每个元素执行一个函数,并返回一个新的数组,其中包含么哦一个元素执行的结果。 语法 const newArray array.map(callback(currentValue, index, arr), thisValue)参数 callback࿱…...

深入浅出 Android AES 加密解密:从理论到实战
深入浅出 Android AES 加密解密:从理论到实战 在现代移动应用中,数据安全是不可忽视的一环。无论是用户隐私保护,还是敏感信息的存储与传输,加密技术都扮演着重要角色。本文将以 AES(Advanced Encryption Standard&am…...

Clickhouse基础(一)
数据存储的目录,在存储数据时是先经过压缩后再存储的,压缩效率很高 操作命令: sudo clickhouse start sudo clickhouse restart sudo clickhouse status进入clickhouse clickhouse-client -mCREATE TABLE db_13.t_assist (modelId UInt64,…...
电脑插入多块移动硬盘后经常出现卡顿和蓝屏
当电脑在插入多块移动硬盘后频繁出现卡顿和蓝屏问题时,可能涉及硬件资源冲突、驱动兼容性、供电不足或系统设置等多方面原因。以下是逐步排查和解决方案: 1. 检查电源供电问题 问题原因:多块移动硬盘同时运行可能导致USB接口供电不足&#x…...

【MATLAB代码】基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),附源代码|订阅专栏后可直接查看
文章所述的代码实现了基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),针对传感器观测数据中存在的脉冲型异常噪声问题,通过非线性加权机制提升滤波器的抗干扰能力。代码通过对比传统KF与MCC-KF在含异常值场景下的表现,验证了后者在状态估计鲁棒性方面的显著优…...

FFmpeg:Windows系统小白安装及其使用
一、安装 1.访问官网 Download FFmpeg 2.点击版本目录 3.选择版本点击安装 注意这里选择的是【release buids】,注意左上角标题 例如我安装在目录 F:\FFmpeg 4.解压 5.添加环境变量 把你解压后的bin目录(即exe所在文件夹)加入系统变量…...

【Post-process】【VBA】ETABS VBA FrameObj.GetNameList and write to EXCEL
ETABS API实战:导出框架元素数据到Excel 在结构工程师的日常工作中,经常需要从ETABS模型中提取框架元素信息进行后续分析。手动复制粘贴不仅耗时,还容易出错。今天我们来用简单的VBA代码实现自动化导出。 🎯 我们要实现什么? 一键点击,就能将ETABS中所有框架元素的基…...
Vue3中的computer和watch
computed的写法 在页面中 <div>{{ calcNumber }}</div>script中 写法1 常用 import { computed, ref } from vue; let price ref(100);const priceAdd () > { //函数方法 price 1price.value ; }//计算属性 let calcNumber computed(() > {return ${p…...

6.9-QT模拟计算器
源码: 头文件: widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QMouseEvent>QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent nullptr);…...
Linux安全加固:从攻防视角构建系统免疫
Linux安全加固:从攻防视角构建系统免疫 构建坚不可摧的数字堡垒 引言:攻防对抗的新纪元 在日益复杂的网络威胁环境中,Linux系统安全已从被动防御转向主动免疫。2023年全球网络安全报告显示,高级持续性威胁(APT)攻击同比增长65%,平均入侵停留时间缩短至48小时。本章将从…...
用 Rust 重写 Linux 内核模块实战:迈向安全内核的新篇章
用 Rust 重写 Linux 内核模块实战:迈向安全内核的新篇章 摘要: 操作系统内核的安全性、稳定性至关重要。传统 Linux 内核模块开发长期依赖于 C 语言,受限于 C 语言本身的内存安全和并发安全问题,开发复杂模块极易引入难以…...
从实验室到产业:IndexTTS 在六大核心场景的落地实践
一、内容创作:重构数字内容生产范式 在短视频创作领域,IndexTTS 的语音克隆技术彻底改变了配音流程。B 站 UP 主通过 5 秒参考音频即可克隆出郭老师音色,生成的 “各位吴彦祖们大家好” 语音相似度达 97%,单条视频播放量突破百万…...
LangChain【6】之输出解析器:结构化LLM响应的关键工具
文章目录 一 LangChain输出解析器概述1.1 什么是输出解析器?1.2 主要功能与工作原理1.3 常用解析器类型 二 主要输出解析器类型2.1 Pydantic/Json输出解析器2.2 结构化输出解析器2.3 列表解析器2.4 日期解析器2.5 Json输出解析器2.6 xml输出解析器 三 高级使用技巧3…...