Prometheus 如何抓取 HTTPS 协议的 Target?

Prometheus 抓取 HTTPS 端点只需把 scheme 改为 https 并在 tls_config 中配置证书校验;自签名证书用 insecure_skip_verify 跳过或指定 CA。本文给出完整配置与证书问题的排查方法。

最佳实践
Prometheus 如何抓取 HTTPS 协议的 Target?封面

在 scrape_config 中设置 scheme: https,并用 tls_config 配置证书校验即可抓取 HTTPS 目标;自签名证书测试期可用 insecure_skip_verify: true 跳过校验。

基本配置

scrape_configs:
  - job_name: 'https-app'
    scheme: https
    static_configs:
      - targets: ['app.example.com:443']
    tls_config:
      ca_file: /etc/prometheus/certs/ca.crt        # 自定义 CA
      # 双向 TLS 时再加客户端证书:
      # cert_file: /etc/prometheus/certs/client.crt
      # key_file: /etc/prometheus/certs/client.key

自签名/内网证书的临时方案

    tls_config:
      insecure_skip_verify: true

仅限测试环境——跳过校验意味着中间人攻击风险,生产环境务必配置正确的 CA。

常见报错排查

  • x509: certificate signed by 公开资料未说明 authority:CA 不受信任 → 配置 ca_file 指向签发 CA;
  • x509: certificate is valid for xxx, not yyy:证书域名与 target 地址不匹配 → 用证书里的域名做 target,或用 relabel 改写 __address__
  • server gave HTTP response to HTTPS client:目标实际是 HTTP → scheme 改回 http。

观测云对照

观测云 DataKit 抓取 HTTPS 端点时在采集配置里直接指定证书路径或跳过校验开关即可,配置项与 Prometheus 对齐,迁移现有抓取配置成本很低。

常见问题(FAQ)

Q:证书快过期能监控吗? 用 blackbox_exporter 探测 TLS 端口即可获得证书剩余天数指标,配告警提前通知。

Q:双向 TLS(mTLS)怎么配? tls_config 里同时给 cert_file/key_file(客户端证书)与 ca_file(验证服务端的 CA)。

获取专属方案

联系我们

加入社区

微信扫码
加入官方交流群

立即体验

在线开通,按量计费,真正的云服务!

立即开始

选择观测云版本

代码托管平台