【发布时间】:2015-02-03 12:45:01
【问题描述】:
我使用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.
我还记录了标题以跟踪此问题:
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
我该如何解决这个问题?
【问题讨论】:
-
我意识到我犯了一个错误,X-Forward-For 应该是 X-Forwarded-For 而 X-Forward-Proto 应该是 X-Forwarded-Proto。但是在我修复之后,问题仍然存在。
标签: tomcat redirect nginx https