2014-12-05 54 views
0

我使用nginx作为前端服务器,并使用tomcat作为后端。客户端通过https方案访问我的网站。例如:将nginx作为反向代理使用不正确的重定向方案

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 

我该如何解决这个问题?

+0

我意识到我犯了一个错误,X- Forward-For应该是X-Forwarded-对于 而X-Forward-Proto应该是X-Forwarded-Proto。但是在我解决之后,问题依然存在。 – guogangj 2014-12-05 11:19:38

回答

1

我得到了解决方案。

nginx.conf

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 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