一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

如何为Nginx设置HTTPS服务的运行状态监测

时间:2026-06-22 09:54:46 编辑:袖梨 来源:一聚教程网

要让Nginx运行状态监测真实反映HTTPS服务健康状况,必须将/status接口部署在listen 443 ssl的server块中,并启用--with-http_ssl_module和--with-http_stub_status_module模块,确保统计TLS握手、加密连接等核心安全指标。

要让 Nginx 的运行状态监测真实反映 HTTPS 服务的健康状况,关键在于把 /status 接口部署在启用 SSL 的 server 块中,并做好访问控制——它不是加个路径就行,而是必须跑在 listen 443 ssl 的上下文里,才能统计 TLS 握手、加密连接等核心安全指标。

确认必要模块已启用

Nginx 必须同时编译进两个模块:--with-http_ssl_module(支持 HTTPS)和 --with-http_stub_status_module(提供状态页)。执行以下命令验证:

  • nginx -V 2>&1 | grep -E "(ssl|stub_status)"

若输出中缺失任一模块,需重新编译安装。例如:

  • CentOS 安装依赖:yum install openssl-devel
  • Ubuntu 安装依赖:apt install libssl-dev
  • 源码编译时加入:./configure --with-http_ssl_module --with-http_stub_status_module

将 status 接口嵌入 HTTPS server 块

不能只配在 HTTP 的 80 端口下——那样统计的是明文流量,无法体现证书校验、TLS 版本协商、SNI 分发等安全行为。必须写在启用 SSL 的 server 块内:

  • 确保已有有效的证书路径:ssl_certificatessl_certificate_key
  • 在该 server 块中添加 location:
location /status {    stub_status on;    access_log off;    allow 127.0.0.1;    allow 192.168.10.0/24;    deny all;}

注意:stub_status on 必须写在 location 内;access_log off 防止高频采集刷爆日志;allow/deny 规则不可省略,严禁直接暴露给公网。

验证与访问方式

配置完成后重载 Nginx:nginx -s reload,然后通过 HTTPS 访问状态页:

  • 本地测试:curl -k https://localhost/status(-k 忽略证书校验)
  • 远程访问需确保 IP 在 allow 列表中,且防火墙放行 443 端口
  • 浏览器访问示例:https://your-domain.com/status(需域名解析到该服务器)

返回内容类似:

Active connections: 3server accepts handled requests 12345 12345 67890Reading: 0 Writing: 1 Waiting: 2

其中 Active connections 是当前加密连接总数,accepts/handled/requests 反映了 HTTPS 连接建立成功率与请求处理量。

对接监控系统(可选扩展)

状态页是纯文本格式,天然适配 Prometheus 等工具采集:

  • nginx-exporter 或自定义 exporter 抓取 /status 页面
  • 提取指标如 nginx_connections_activenginx_requests_total
  • 结合 TLS 相关日志字段(如 $ssl_protocol$ssl_cipher)可进一步分析加密质量

不复杂但容易忽略:只有 HTTPS server 块里的 status,才真正代表你线上加密服务的实时负载与连接健康度。

热门栏目