除了basic auth, 如果希望更安全,可以使用https加密

前提

  1. 有公网域名
  2. 有域名证书,可以申请个免费的二级域名证书,申请成功后获取example.com.crt和example.com.key

有可以生成自签名证书和私钥,本地测试时可以用这种方式

  1. mkdir -p /root/certs/example.com && cd /root/certs/example.com
  2. openssl req \
  3. -x509 \
  4. -newkey rsa:4096 \
  5. -nodes \
  6. -keyout example.com.key \
  7. -out example.com.crt

建议将 Prometheus 与反向代理结合使用,并在代理层应用 TLS。可以使用任何喜欢的反向代理,在本指南中,我们将提供一个 nginx 示例。

Nginx 配置

nginx 创建站点 prometheus.mafeifan.com.conf

  1. http {
  2. server {
  3. listen 443 ssl;
  4. server_name prometheus.mafeifan.com;
  5. ssl_certificate /etc/nginx/certs/prometheus.mafeifan.com.crt;
  6. ssl_certificate_key /etc/nginx/certs/prometheus.mafeifan.com.key;
  7. location /prometheus {
  8. proxy_pass http://localhost:9090/;
  9. }
  10. }
  11. }
  12. events {}

Prometheus 配置

加入 web.external-url 参数并将前缀设置为”/“

  1. prometheus \
  2. --config.file=/path/to/prometheus.yml \
  3. --web.external-url=http://prometheus.mafeifan.com\prometheus \
  4. --web.route-prefix="/"

测试

curl -k https://prometheus.mafeifan.com\prometheus/api/v1/label/job/values

参考

https://hulining.gitbook.io/prometheus/guides/tls-encryption
[

](https://example.com/prometheus/api/v1/label/job/values)