Prometheus 如何抓取 HTTPS 协议的 Target?
Prometheus 抓取 HTTPS 端点只需把 scheme 改为 https 并在 tls_config 中配置证书校验;自签名或内部 CA 证书应配置可信 CA;跳过校验仅用于受控临时诊断。本文给出完整配置与证书问题的排查方法。
在 scrape_config 中设置 scheme: https,并用 tls_config 配置证书校验即可抓取 HTTPS 目标;自签名或内部 CA 也应优先用 ca_file 建立受信任链。
基本配置
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 unknown authority:CA 不受信任 → 配置ca_file指向签发 CA;x509: certificate is valid for xxx, not yyy:证书域名与 target 地址不匹配 → 用证书里的域名做 target,或明确用 tls_config.server_name 指定证书校验名称/SNI;改写 address 会改变连接地址,不能只当作改证书名称;server gave HTTP response to HTTPS client:目标实际是 HTTP → scheme 改回 http。
常见问题(FAQ)
Q:证书快过期能监控吗? 用 blackbox_exporter 探测 TLS 端口可读取 probe_ssl_earliest_cert_expiry(Unix 秒时间戳),用 (probe_ssl_earliest_cert_expiry - time()) / 86400 计算剩余天数,配告警提前通知。
Q:双向 TLS(mTLS)怎么配? tls_config 里同时给 cert_file/key_file(客户端证书)与 ca_file(验证服务端的 CA)。