如何让 nginx 转发 HTTP_X_FORWARDED_PROTO 标头?

Ech*_*ica 27 nginx

我正在链接我的设置

nginx > apache/php
Run Code Online (Sandbox Code Playgroud)

haproxy > nginx > apache/php
Run Code Online (Sandbox Code Playgroud)

(使用 haproxy 1.5-dev18 并编译了 ssl 支持)

nginx 和 haproxy 都已正确设置以设置 HTTP_X_FORWARDED_PROTO 标头。但是,当 nginx 从 haproxy 获取 ssl 流量时,它会将连接视为 http 并将标头设置为如此。

如何设置 nginx 以转发 HTTP_X_FORWARDED_PROTO 标头(如果存在),否则继续根据连接设置它?

Ech*_*ica 42

我想出了如何解决这个问题。问题是 nginx 覆盖了 haproxy 在我配置的这一行上设置的标头:

proxy_set_header X-Forwarded-Proto $scheme;
Run Code Online (Sandbox Code Playgroud)

我通过添加以下内容来修复它:

map $http_x_forwarded_proto $thescheme {
     default $scheme;
     https https;
 }   
Run Code Online (Sandbox Code Playgroud)

并更改 proxy_set_header 行以使用新方案:

proxy_set_header X-Forwarded-Proto $thescheme;
Run Code Online (Sandbox Code Playgroud)

  • `$scheme` 与 `$thescheme`。 (2认同)

job*_*wat 15

我对 AWS ELB 也有同样的需求

这是我的解决方案:

proxy_set_header        X-Forwarded-Proto $http_x_forwarded_proto;
Run Code Online (Sandbox Code Playgroud)

  • 你的回答刚刚解决了一个我已经困扰了 3 天的问题!所有其他关于 aws 负载均衡器主题的帖子都带有 https 倡导者 `proxy_set_header X-Forwarded-Proto $scheme;`,这不起作用 (2认同)