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

NLB后ElasticBeanstalk Tomcat环境中$http_x_forwarded_proto为空问题

解决Elastic Beanstalk中Nginx $http_x_forwarded_proto为空的问题

当前配置中尝试通过proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;传递协议头,但$http_x_forwarded_proto值为空,原因是Elastic Beanstalk负载均衡器传递的X-Forwarded-Proto头未被正确处理,或直接访问Nginx时该头不存在。

解决方案

修改.platform/nginx/conf.d/elasticbeanstalk/00_application.conf配置,补充协议头的 fallback 逻辑,同时优化客户端IP传递:

location / {
    proxy_set_header X-Real-IP $remote_addr;
    # 保留完整的客户端IP链,替代原有的$remote_addr
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    # 优先使用负载均衡传来的X-Forwarded-Proto,无值时用当前请求协议
    set $forwarded_proto $http_x_forwarded_proto;
    if ($forwarded_proto = "") {
        set $forwarded_proto $scheme;
    }
    proxy_set_header X-Forwarded-Proto $forwarded_proto;
    
    proxy_set_header X-Request-URI $request;
    proxy_set_header X-Request-PATH $request_uri;
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version  1.1;
}
client_max_body_size 20M;

关键修改说明

  1. X-Forwarded-For优化:使用$proxy_add_x_forwarded_for替代$remote_addr,可以保留负载均衡器传递的原始客户端IP,而非仅记录负载均衡的IP。
  2. X-Forwarded-Proto补全:通过set指令和简单判断,确保无论请求来自负载均衡还是直接访问Nginx,X-Forwarded-Proto都有有效值,避免为空。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 18:12:51