nginx 代理https后,應用redirect https變成http --轉


原文地址:http://blog.sina.com.cn/s/blog_56d8ea900101hlhv.html

情況說明
nginx配置https,tomcat正常http接受nginx轉發。
nginx 代理https后,(java代碼redirect地址)應用redirect https變成http

情況類似

http://2hei.net/mt/2010/02/request-getscheme-cannt-get-https.html

http://yywudi.info/nginx-https-400-bad-request-solution/

原因分析:

經過nginx代理后用的spring mvc的redirect,

其中: request.getScheme() return http but not https.

瀏覽器調整的地址變成http

解決辦法:http://han.guokai.blog.163.com/blog/static/136718271201211631456811/

在代理模式下,Tomcat 如何識別用戶的直接請求(URL、IP、https還是http )?
在透明代理下,如果不做任何配置Tomcat 認為所有的請求都是 Nginx 發出來的,這樣會導致如下的錯誤結果:

    request.getScheme()  //總是 http,而不是實際的http或https
    request.isSecure()  //總是false(因為總是http)
    request.getRemoteAddr()  //總是 nginx 請求的 IP,而不是用戶的IP
    request.getRequestURL()  //總是 nginx 請求的URL 而不是用戶實際請求的 URL
    response.sendRedirect( 相對url )  //總是重定向到 http 上 (因為認為當前是 http 請求)

如果程序中把這些當實際用戶請求做處理就有問題了。解決方法很簡單,只需要分別配置一下 Nginx 和 Tomcat 就好了,而不用改程序。
配置 Nginx 的轉發選項:

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;

配置Tomcat server.xml 的 Engine 模塊下配置一個 Value:
Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/

配置雙方的 X-Forwarded-Proto 就是為了正確地識別實際用戶發出的協議是 http 還是 https。X-Forwarded-For 是為了獲得實際用戶的 IP。
這樣以上5項測試就都變為正確的結果了,就像用戶在直接訪問 Tomcat 一樣。

后記:
上面https到http解決辦法對context引導還是會出問題,
比如輸入https://xxxx/signet 會轉向到http://xxx/signet/ ,
http情況下:輸入http://xxxx/signet會返回一個302到http://xxxx/signet/然后正常訪問。

解決辦法:
# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;

http://serverfault.com/questions/145383/proxy-https-requests-to-a-http-backend-with-nginx
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-with-ssl-as-a-reverse-proxy-for-jenkins
http://www.cyberciti.biz/faq/linux-unix-nginx-redirect-all-http-to-https/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM