gpt4 book ai didi

tomcat - 使用 nginx 作为反向代理的重定向方案不正确

转载 作者:行者123 更新时间:2023-11-28 21:58:26 29 4
gpt4 key购买 nike

我使用nginx作为前端服务器,tomcat作为后端。客户端通过 https 方案访问我的站点。例如:

https://example.com/app/test

Nginx 收到这个并通过这个本地地址将请求传递给 tomcat:

http://localhost:8080/app/test

我的 nginx 配置:

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto $scheme;
proxy_pass http://localhost:8080;
}

这很好用。但是,当我尝试使用重定向 (http 302) 时,它总是将 https 变为 http。这是我的重定向代码:

response.sendRedirect("/app/redirect-test");

我期望将浏览器重定向到:

https://example.com/app/redirect-test

但它总是将浏览器重定向到:

http://example.com/app/redirect-test.

我还记录了 header 以跟踪此问题:

host : example.com
x-real-ip : 180.168.202.246
x-forward-for : 180.168.202.246
x-forward-proto : https
connection : close
......

但我还是想不通。然后我尝试记录 request.getRequestURL(),然后我得到了这个结果:

http://example.com/app/test

我该如何解决这个问题?

最佳答案

我得到了解决方案。

nginx 配置文件

location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8080;
}

修改Tomcat的server.xml Host部分

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- blah blah blah -->
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
</Host>

到这里查看详情:Tomcat Doc

关于tomcat - 使用 nginx 作为反向代理的重定向方案不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27314332/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com