You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ingress配置问题:访问Prometheus时重定向路径异常如何解决?

解决Prometheus Ingress重定向丢失子路径的问题

问题根源

Prometheus默认会将根路径请求重定向到/graph,但它不知道自己是通过/prometheus子路径对外暴露的,因此生成的重定向URL不会带上/prometheus前缀;同时当前Ingress仅做了请求路径的转发,未处理Prometheus返回的重定向响应头。

推荐解决方案(从Prometheus层面修正)

这种方法能从根源解决问题,同时避免页面静态资源路径错误:

  1. 修改Prometheus启动参数,添加--web.external-url=http://localhost/prometheus(生产环境替换为实际外部访问域名+/prometheus):

    • 若用Helm部署,在values.yaml中配置:
      server:
        extraArgs:
          web.external-url: "http://localhost/prometheus"
      
    • 若用自定义Deployment,修改容器command字段:
      containers:
      - name: prometheus
        image: prom/prometheus:latest
        command:
        - "/bin/prometheus"
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--web.external-url=http://localhost/prometheus"
        # 保留其他原有参数
      
  2. 调整Ingress配置,适配路径前缀转发:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$2
      name: prometheus-ingress
      namespace: prometheus
    spec:
      ingressClassName: nginx
      rules:
      - http:
          paths:
          - backend:
              service:
                name: prometheus-service
                port:
                  number: 9090
            path: /prometheus(/|$)(.*)
            pathType: Prefix
    

备选方案(仅通过Ingress修正重定向)

若无法修改Prometheus配置,可通过Nginx Ingress的配置片段修改响应头:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/configuration-snippet: |
      # 处理/prometheus末尾无斜杠的情况
      if ($request_uri = "/prometheus") {
          return 302 /prometheus/;
      }
      # 替换重定向URL中的前缀
      proxy_redirect http://localhost/graph http://localhost/prometheus/graph;
      proxy_redirect http://localhost/ http://localhost/prometheus/;
  name: prometheus-ingress
  namespace: prometheus
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - backend:
          service:
            name: prometheus-service
            port:
              number: 9090
        path: /prometheus/?(.*)
        pathType: Prefix

注意:该方案仅修正重定向,可能导致Prometheus页面静态资源(CSS、JS)路径错误,出现样式失效问题,优先推荐第一种方案。

内容的提问来源于stack exchange,提问作者Stephen Zhou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:10:23