【发布时间】:2020-06-22 06:54:44
【问题描述】:
我在标准反向代理场景中使用 nginx,将所有对 /auth 的请求传递给另一台主机,但是我正在尝试使用非标准端口。
我的最终目标是将X-Forwarded-Port 标头设置为请求进入的端口。
这是我在 nginx.conf 中的位置块:
location /auth/ { proxy_pass http://otherhost:8090; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port <VAR>; }
这个 nginx 运行在一个 docker 容器中,该容器被配置为将请求从 8085 转发到容器中的 80,这样 nginx 进程就在监听 80:
0.0.0.0:8085->80/tcp
当我点击网址时:
我已正确重定向到 http://otherhost:8090,但 X-Forwarded-Port 标头丢失或错误。
我在原始块中有<VAR>,我尝试了以下方法:
$server_port- 这是 nginx 正在监听的端口 (80),而不是 请求端口。$pass_port- 在我的设置中似乎为空,所以 nginx 删除了 标题。$http_port- 这是每个请求的随机端口。$remote_port- 这是每个请求的随机端口。
我可以在部署时更改我的配置以硬编码到传入请求的已知端口,但理想情况下,我可以更改前端端口而不对 nginx 配置进行任何更改。
我搜索了the nginx variable list,但找不到像$request_port 这样的东西。有什么方法可以实现我的意图吗?
【问题讨论】:
-
你试过
$remote_port吗?除此之外,我很难理解 nginx 正在侦听的端口 ($server_port) 与请求的端口有何不同。 -
@Mehdi - $remote_port 似乎也是每个请求随机的。请求到达 docker 主机的 8085 端口,然后转发到 80 端口的容器,所以 nginx 监听 80 但 URL 中的端口是 8085。
标签: docker nginx reverse-proxy nginx-reverse-proxy x-forwarded-for