最近在作一個項目, 架構上使用了 Nginx +tomcat 集羣, 且nginx下配置了SSL,tomcat no SSL,項目使用https協議html
![](http://static.javashuo.com/static/loading.gif)
可是,明明是https url請求,發現 log裏面,java
0428 15:55:55 INFO (PaymentInterceptor.java:44) preHandle() - requestStringForLog: {
"request.getRequestURL():": "http://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6",
"request.getMethod:": "GET",
"_parameterMap": {
"id": ["212"],
"s": ["a84485e0985afe97fffd7fd7741c93851d83a4f6"]
}
}
request.getRequestURL() 輸出出來的 一直是
http://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6
可是瀏覽器中的URL倒是
https://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6
瞬間要顛覆個人Java觀
,API上寫得很清楚:nginx
getRequestURL():算法
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
也就是說, getRequestURL() 輸出的是不帶query string的路經(含協議 端口 server path等信息).apache
![](http://static.javashuo.com/static/loading.gif)
而且,還發現api
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 就行了,而不用改程序。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;
proxy_set_header X-Forwarded-Proto $scheme;app
配置Tomcat server.xml 的 Engine 模塊下配置一個 Valve:
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https"/>
配置雙方的 X-Forwarded-Proto 就是爲了正確地識別實際用戶發出的協議是 http 仍是 https。
這樣以上5項測試就都變爲正確的結果了,就像用戶在直接訪問 Tomcat 同樣。
關於 RemoteIpValve,有興趣的同窗能夠閱讀下 doc
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/RemoteIpValve.html
Tomcat port of mod_remoteip, this valve replaces the apparent client remote IP address and hostname for the request with the IP address list presented by a proxy or a load balancer via a request headers (e.g. "X-Forwarded-For").
Another feature of this valve is to replace the apparent scheme (http/https) and server port with the scheme presented by a proxy or a load balancer via a request header (e.g. "X-Forwarded-Proto").
看了下他們的源碼,比較簡單,在各類框架,各類算法面前,這個類對性能影響很小
- 若是沒有配置protocolHeader 屬性, 什麼都不作.
- 若是配置了protocolHeader,可是request.getHeader(protocolHeader)取出來的值是null,什麼都不作
- 若是配置了protocolHeader,可是request.getHeader(protocolHeader)取出來的值(忽略大小寫)是 配置的protocolHeaderHttpsValue(默認https),scheme設置爲https,端口設置 爲 httpsServerPort
- 其餘設置爲 http
if (protocolHeader != null) {
String protocolHeaderValue = request.getHeader(protocolHeader);
if (protocolHeaderValue == null) {
// don't modify the secure,scheme and serverPort attributes
// of the request
} else if (protocolHeaderHttpsValue.equalsIgnoreCase(protocolHeaderValue)) {
request.setSecure(true);
// use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0
request.getCoyoteRequest().scheme().setString("https");
request.setServerPort(httpsServerPort);
} else {
request.setSecure(false);
// use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0
request.getCoyoteRequest().scheme().setString("http");
request.setServerPort(httpServerPort);
}
}
轉載地址:http://www.cnblogs.com/interdrp/p/4881785.html