Nginx需启用SSL与stub_status模块,在HTTPS server块中配置/status接口并设访问控制,返回Active connections等5项纯文本指标供Prometheus等监控系统采集。
要让 Nginx 不仅支持 HTTPS,还能实时导出加密连接的运行指标(比如当前活跃连接数、握手成功率、请求吞吐量等),关键不是“导出”状态页本身,而是
正确启用 stub_status 模块 + 合理暴露 HTTPS 状态端点 + 配合安全访问控制
。它不生成日志文件或 CSV,而是通过一个轻量 HTTP 接口返回纯文本数据,方便脚本或监控系统(如 Prometheus、Zabbix)抓取解析。
确保 Nginx 已编译含 SSL 和 stub_status 模块
很多默认安装包会缺其中一个模块。执行以下命令检查:
nginx-V 2>&1 | grep -E "(ssl|stub_status)"
若输出中没有
--with-http_ssl_module
或
--with-http_stub_status_module
,需重新编译 Nginx。例如:
下载源码后进入目录,运行:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
确保系统已装 openssl-devel(CentOS)或 libssl-dev(Ubuntu)
执行
make && make install
覆盖原二进制
在 HTTPS server 块内配置 status 接口
不能只配在 HTTP 的 80 端口 server 块里——那样看到的是非加密流量指标。必须把
放进
的 server 块中,才能统计真实 TLS 连接行为:
server {listen 443 ssl;server_name example.com;ssl_certificate /path/to/fullchain.pem;ssl_certificate_key /path/to/privkey.pem;# 其他 SSL 参数...location /status {stub_status on;access_log off;allow 127.0.0.1;allow 192.168.10.0/24;deny all;}}
注意:
•
必须写在 location 内,且不能加引号
•
避免刷爆日志
•
控制谁可访问,禁止公网裸露
Nginx
在宝塔面板中轻松管理Nginx高性能Web服务器。提供可视化配置反向代理、负载均衡、SSL证书及HTTP缓存功能,一键优化高并发性能,助您高效搭建稳定、快速的网站运行环境。
下载
理解并利用 status 返回的 5 项核心指标
访问
(需满足 allow 规则)将返回类似:
Active connections: 12server accepts handled request12456 12456 28934Reading: 2 Writing: 5 Waiting: 5
每行含义:
Active connections
:当前所有处于 ESTABLISHED 状态的 TCP 连接数(含 TLS 握手未完成的)
accepts
:Nginx 自启动以来接受的总连接数(TCP accept() 成功次数)
handled
:成功完成 TLS 握手并进入 HTTP 处理流程的连接数(= accepts 说明无 handshake 失败)
requests
:在 handled 连接上处理的总 HTTP 请求次数(一个连接可复用多次)
Reading/Writing/Waiting
:当前各阶段连接数 —— Reading 是正在读取请求头,Writing 是正在发送响应,Waiting 是 keep-alive 空闲等待
对接监控系统实现自动化采集
status 页面是纯文本、无认证、无 CORS,适合 curl + shell 或 exporter 抓取:
用 curl 定时采集(示例):
curl -k -s "https://localhost/status" | awk 'NR==1 {print $3}'
→ 提取 Active connections
Prometheus 用户可直接部署
nginx-lua-prometheus
(需 Lua 支持),或使用官方推荐的
nginx-prometheus-exporter
(独立进程,代理转发 /status)
Zabbix 可通过 “Simple check” 类型的 item,类型选 “HTTP agent”,URL 填
,再用正则提取字段
不复杂但容易忽略:务必在采集脚本中加
(跳过证书校验)或配置信任 CA,否则自签名或内网证书会导致采集失败。
location /statuslisten 443 sslstub_status onaccess_log offallow/denyhttps://example.com/statushttps://example.com/status-k