2014-12-05 116 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