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

AWS EKS环境Nginx Ingress后端307 HTTP重定向未转HTTPS问题求助

问题根因

你配置的nginx.ingress.kubernetes.io/ssl-redirect和nginx.ingress.kubernetes.io/force-ssl-redirect规则,仅作用于客户端入站的HTTP请求,会直接拦截HTTP请求返回301/302跳转到HTTPS,但是不会修改后端服务返回的重定向响应内容。
你的后端gunicorn服务本身运行在HTTP环境下,无法感知外层Ingress的HTTPS终止逻辑,生成307重定向时会默认填充http://协议的Location头,Ingress控制器默认直接透传该响应给前端,就会触发HTTPS页面加载HTTP资源的Mixed Block错误。

解决方案

方案1:Ingress侧自动重写重定向协议(最便捷,无需修改后端配置)

在Ingress的annotations中添加如下两个配置即可,对301/302/307/308所有重定向状态码都生效:

nginx.ingress.kubernetes.io/proxy-redirect-from: http://kube.example.com/
nginx.ingress.kubernetes.io/proxy-redirect-to: https://kube.example.com/

如果需要适配所有域名,可以用变量写法:

nginx.ingress.kubernetes.io/proxy-redirect-from: http://$host/
nginx.ingress.kubernetes.io/proxy-redirect-to: https://$host/

方案2:后端服务配置信任代理(从根源解决)

给gunicorn添加启动参数,信任Ingress传递的X-Forwarded-*请求头,gunicorn识别到客户端实际使用HTTPS协议后,生成重定向时会自动用https://前缀:

# *可以替换为Ingress控制器的Pod网段,提升安全性
--forwarded-allow-ips="*"

末尾斜杠移除配置修正

你之前写的location-snippet语法错误,正确的重写规则可以直接用rewrite-snippet实现,无需额外定义location块:

metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-snippet: |
      # 仅匹配/api/v1/路径下的末尾斜杠移除
      rewrite ^/api/v1/(.*)/$ /api/v1/$1 permanent;

验证方法

配置完成后执行curl命令验证重定向头:

curl -v https://kube.example.com/api/v1/你的测试路径

检查返回的Location响应头是否为https://开头即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 23:45:02