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.mzph.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中,迭代器通常以一个函…