首页主机资讯如何在Ubuntu上监控Nginx SSL

如何在Ubuntu上监控Nginx SSL

时间2025-12-03 11:21:04发布访客分类主机资讯浏览356
导读:Ubuntu上监控Nginx SSL的实用方案 一 监控目标与总体思路 覆盖三类关键指标: 证书有效期(本地证书文件与远端站点证书); TLS握手与链完整性(协议、套件、证书链是否可用); Nginx运行状态与连接(进程存活、端口监听、...

Ubuntu上监控Nginx SSL的实用方案

一 监控目标与总体思路

  • 覆盖三类关键指标:
    1. 证书有效期(本地证书文件与远端站点证书);
    2. TLS握手与链完整性(协议、套件、证书链是否可用);
    3. Nginx运行状态与连接(进程存活、端口监听、活动连接、读写状态)。
  • 建议组合使用:命令行快速核查 + 脚本化巡检 + 可视化监控平台告警,实现近实时与历史趋势的统一观测。

二 命令行快速核查

  • 本地证书有效期
    • 查看本地证书与私钥是否匹配:
      • 证书到期时间:openssl x509 -in /path/to/cert.pem -noout -dates
      • 私钥与证书匹配:openssl x509 -noout -modulus -in /path/to/cert.pem | openssl md5openssl rsa -noout -modulus -in /path/to/key.pem | openssl md5,两者哈希应一致。
  • 远端站点证书与握手信息
    • 指定SNI获取远端证书到期时间:echo | openssl s_client -connect example.com:443 -servername example.com 2> /dev/null | openssl x509 -noout -dates
    • 查看握手与链信息:openssl s_client -connect example.com:443 -servername example.com
  • Nginx运行状态与连接
    • 进程与端口:systemctl is-active nginx & & ss -lntp | grep ':443\|:80'
    • 基础连接指标(需启用 stub_status):curl -s http://127.0.0.1/nginx_status
      以上命令覆盖证书有效期、握手状态与Nginx运行状态的基础核查,适合作为日常巡检与故障定位的第一步。

三 脚本化巡检与阈值告警

  • 示例脚本 check_ssl.sh(本地证书与远端站点)
    • 功能:检查本地证书到期天数与远端证书到期天数,低于阈值则输出非零退出码,便于接入 Nagios/Zabbix/系统d 告警。
    • 用法:bash check_ssl.sh /etc/nginx/ssl/site.crt https://example.com 30
    • 脚本内容:
      #!/usr/bin/env bash
      set -Eeuo pipefail
      
      CERT_FILE="$1"
      URL="$2"
      WARN_DAYS="${
      3:-30}
          "
      
      now_ts=$(date +%s)
      
      # 本地证书到期天数
      if [[ -f "$CERT_FILE" ]];
           then
        exp_str=$(openssl x509 -in "$CERT_FILE" -noout -enddate | cut -d= -f2)
        exp_ts=$(date -d "$exp_str" +%s)
        days_left=$(( (exp_ts - now_ts) / 86400 ))
        echo "local_cert_days_left=$days_left"
        if (( days_left <
          = WARN_DAYS ));
           then
          echo "CRITICAL: $CERT_FILE 剩余 $days_left 天"
          exit 2
        elif (( days_left <
          = WARN_DAYS * 2 ));
       then
          echo "WARNING: $CERT_FILE 剩余 $days_left 天"
          exit 1
        else
          echo "OK: $CERT_FILE 剩余 $days_left 天"
          exit 0
        fi
      fi
      
      # 远端证书到期天数(SNI)
      domain=$(echo "$URL" | awk -F/ '{
      print $3}
          ')
      exp_str=$(echo | openssl s_client -connect "$domain:443" -servername "$domain" 2>
          /dev/null \
                | openssl x509 -noout -enddate | cut -d= -f2)
      exp_ts=$(date -d "$exp_str" +%s)
      days_left=$(( (exp_ts - now_ts) / 86400 ))
      echo "remote_cert_days_left=$days_left"
      if (( days_left <
          = WARN_DAYS ));
           then
        echo "CRITICAL: $URL 剩余 $days_left 天"
        exit 2
      elif (( days_left <
          = WARN_DAYS * 2 ));
           then
        echo "WARNING: $URL 剩余 $days_left 天"
        exit 1
      else
        echo "OK: $URL 剩余 $days_left 天"
        exit 0
      fi
      
  • 接入系统定时任务(示例)
    • 每天检查并写入日志:0 2 * * * root /usr/local/bin/check_ssl.sh /etc/nginx/ssl/site.crt https://example.com 30 > > /var/log/ssl_check.log 2> & 1
  • 说明
    • 远端检查依赖网络连通与SNI;若需批量域名,可将域名列表放入文件循环调用脚本。
    • 证书替换后建议执行 sudo systemctl reload nginx 使新证书生效,无需重启服务。

四 可视化监控与告警集成

  • Prometheus + Blackbox Exporter + Nginx Exporter
    • 证书探测:使用 Blackbox Exporter 对目标域名执行 TLS/HTTPS 探测,抓取证书到期时间等指标,配置告警规则(如剩余天数 < 30 天)。
    • Nginx指标:启用 ngx_http_stub_status_module,使用 Nginx Exporter 暴露连接数、读写状态等,结合 Grafana 面板展示趋势。
  • Zabbix
    • 方式一:使用 Zabbix Agent 用户参数 执行上述脚本,依据退出码与输出设置触发器(Warning/Critical)。
    • 方式二:通过 Zabbix Agent 主动模式 或外部检查执行脚本并上报数据。
  • 告警建议
    • 证书:剩余天数阈值建议 30/7/1 天 多级告警;
    • 握手:探测失败、链不完整、协议/套件不合规立即告警;
    • Nginx:进程宕机、端口未监听、活动连接异常波动告警。
      上述方案覆盖从黑盒探测到白盒指标的完整链路,适合规模化与长期趋势观测。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 如何在Ubuntu上监控Nginx SSL
本文地址: https://pptw.com/jishu/762176.html
Ubuntu Nginx SSL如何配置Gzip压缩 Ubuntu Nginx SSL如何配置SPDY

游客 回复需填写必要信息