Kubernetes Nginx Ingress根路径重定向遇循环错误求助
解决Ingress重定向循环问题
原Ingress配置
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress labels: app.kubernetes.io/managed-by: Helm annotations: kubernetes.io/ingress.class: nginx meta.helm.sh/release-name: ingress nginx.ingress.kubernetes.io/configuration-snippet: | location ~ favicon.ico { log_not_found off; } nginx.ingress.kubernetes.io/cors-allow-headers: content-type, x-att-timezone nginx.ingress.kubernetes.io/cors-allow-methods: GET, POST, PUT, DELETE, OPTIONS nginx.ingress.kubernetes.io/cors-allow-origin: '*' nginx.ingress.kubernetes.io/cors-expose-headers: 'x-att-userrole, x-att-userdetails, x-att-userid, xatt-location ' nginx.ingress.kubernetes.io/enable-cors: 'true' nginx.ingress.kubernetes.io/force-ssl-redirect: 'true' nginx.ingress.kubernetes.io/proxy-body-size: 10000m nginx.ingress.kubernetes.io/proxy-connect-timeout: '6000000' nginx.ingress.kubernetes.io/proxy-read-timeout: '6000000' nginx.ingress.kubernetes.io/proxy-send-timeout: '6000000' nginx.ingress.kubernetes.io/use-regex: 'true' spec: tls: - hosts: - st-my-doamin.com secretName: ingress rules: - host: st-my-doamin.com http: paths: - path: /rootpath/.* pathType: Prefix backend: service: name: someService port: number: 80
需求与问题
需要实现:访问st-my-doamin.com/rootpath时自动重定向到st-my-doamin.com/rootpath/login,但配置后出现重定向循环错误:
This page isn’t working st-my-doamin.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
问题原因
重定向循环的核心是重定向规则未排除目标路径,如果之前的配置是对/rootpath做前缀或模糊匹配的重定向,会导致/rootpath/login也被匹配到规则,触发重复重定向,形成循环。
解决方案
修改Ingress的configuration-snippet,添加精确匹配的重定向规则,确保只有请求路径恰好是/rootpath时才执行重定向:
修改后的完整Ingress配置
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress labels: app.kubernetes.io/managed-by: Helm annotations: kubernetes.io/ingress.class: nginx meta.helm.sh/release-name: ingress nginx.ingress.kubernetes.io/configuration-snippet: | location ~ favicon.ico { log_not_found off; } # 添加精确匹配的重定向规则 location = /rootpath { return 302 /rootpath/login; } nginx.ingress.kubernetes.io/cors-allow-headers: content-type, x-att-timezone nginx.ingress.kubernetes.io/cors-allow-methods: GET, POST, PUT, DELETE, OPTIONS nginx.ingress.kubernetes.io/cors-allow-origin: '*' nginx.ingress.kubernetes.io/cors-expose-headers: 'x-att-userrole, x-att-userdetails, x-att-userid, xatt-location ' nginx.ingress.kubernetes.io/enable-cors: 'true' nginx.ingress.kubernetes.io/force-ssl-redirect: 'true' nginx.ingress.kubernetes.io/proxy-body-size: 10000m nginx.ingress.kubernetes.io/proxy-connect-timeout: '6000000' nginx.ingress.kubernetes.io/proxy-read-timeout: '6000000' nginx.ingress.kubernetes.io/proxy-send-timeout: '6000000' nginx.ingress.kubernetes.io/use-regex: 'true' spec: tls: - hosts: - st-my-doamin.com secretName: ingress rules: - host: st-my-doamin.com http: paths: - path: /rootpath/.* pathType: Prefix backend: service: name: someService port: number: 80
关键说明
- 使用
location = /rootpath的精确匹配语法,仅当请求路径完全等于/rootpath时才触发重定向。 /rootpath/login不会被该规则匹配,因此不会触发二次重定向,彻底解决循环问题。
内容的提问来源于stack exchange,提问作者ITBYD
相关产品推荐
相关产品推荐

