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

GKE中Nginx Ingress无法将HTTP重定向至HTTPS求助

解决GKE中Nginx Ingress无法自动HTTP重定向到HTTPS的问题

你的问题根源在于Ingress配置中**ingressClassName的位置错误**——它被错误地放在了metadata.annotations里,而正确的位置应该是在spec字段下。这个错误会导致Nginx Ingress Controller无法识别你的配置注解,包括force-ssl-redirect,自然无法触发重定向。

修正后的Ingress配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: django-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.global-static-ip-name: django-ip
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    nginx.ingress.kubernetes.io/use-forwarded-headers: "true"
spec:
  ingressClassName: "nginx"  # 移至spec字段下
  tls:
    - hosts:
        - example-django.com
      secretName: django-certs
  defaultBackend:
    service:
      name: django-svc
      port: 
        name: django-core-http
  rules:
    - host: example-django.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: django-svc
                port:
                  name: django-core-http

额外验证步骤

  1. 确认Nginx Ingress Controller正常运行:执行kubectl get pods -n ingress-nginx,检查是否有处于Running状态的控制器Pod。
  2. 应用修正后的配置:执行kubectl apply -f <你的Ingress配置文件名>.yaml,等待Ingress资源更新生效。
  3. 测试重定向效果:用curl -v http://example-django.com,查看响应是否包含308 Permanent Redirect并指向HTTPS地址。

如果问题仍存在,可查看Nginx控制器日志排查配置加载问题:kubectl logs -n ingress-nginx <nginx-controller-pod名称>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 05:51:08