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

Ingress-Nginx Controller全局SSL/HTTPS重定向配置问题求助

Ingress-Nginx全局SSL重定向失效问题排查与修复

核心问题分析

全局配置force-ssl-redirect: "true"不生效,但单个Ingress加注解nginx.ingress.kubernetes.io/force-ssl-redirect: "true"正常,主要问题出在端口映射、NLB协议配置、转发头部信任这三个环节:

  • Service的HTTP端口映射错误,导致Ingress Controller无法接收HTTP请求执行重定向逻辑
  • AWS NLB与Ingress Controller的后端协议不匹配,使控制器误判请求协议
  • 未正确配置NLB的IP信任范围,控制器不认可转发头部的原始请求协议

修复步骤

1. 修正Service端口映射

将Service中HTTP端口的targetPort改为Ingress Controller容器默认的HTTP端口(http对应容器内部80端口),确保控制器能接收HTTP请求:

apiVersion: v1
kind: Service
metadata:
  # 保持原有注解和标签不变
spec:
  ports:
  - appProtocol: http
    name: http
    port: 80
    protocol: TCP
    targetPort: http  # 替换原有的tohttps
  - appProtocol: https
    name: https
    port: 443
    protocol: TCP
    targetPort: https
  # 保持其他配置不变

2. 调整NLB后端协议配置

根据你的SSL终止架构选择对应配置:

  • SSL终止在NLB:NLB处理HTTPS后转发明文HTTP到控制器,替换原有全局后端协议注解,按端口指定协议:
    metadata:
      annotations:
        # 删除原有的service.beta.kubernetes.io/aws-load-balancer-backend-protocol注解
        service.beta.kubernetes.io/aws-load-balancer-target-group-attributes: "backend_protocol=http:80,backend_protocol=ssl:443"
        # 保持其他注解不变
    
  • SSL终止在Ingress Controller:NLB仅透传TCP流量,保留backend-protocol: ssl,但需确保控制器443端口有有效证书(通过Ingress的tls字段或全局证书配置)。

3. 配置转发头部信任范围

将proxy-real-ip-cidr设置为NLB所在VPC的CIDR范围(或AWS NLB官方全局CIDR),确保控制器信任NLB发送的X-Forwarded-Proto头部:

apiVersion: v1
data:
  allow-snippet-annotations: "true"
  proxy-real-ip-cidr: "10.0.0.0/16"  # 替换为你的NLB所在VPC的CIDR
  use-forwarded-headers: "true"
  proxy-body-size: "0"
  force-ssl-redirect: "true"
kind: ConfigMap
# 保持原有元数据不变

4. 检查Ingress对象注解

确保所有Ingress对象未设置nginx.ingress.kubernetes.io/force-ssl-redirect: "false",该注解会覆盖全局配置。


验证操作

  1. 应用修改并重启控制器:
    kubectl apply -f ingress-nginx-service.yaml
    kubectl apply -f ingress-nginx-configmap.yaml
    kubectl rollout restart deployment ingress-nginx-controller -n ingress-nginx
    
  2. 测试重定向:
    curl -v http://something_in_my_cluster.com
    
    应返回301/302状态码,重定向到HTTPS地址。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 05:59:57