Nginx SSL+tomcat叢集,request.getScheme() 取到https正確的協議

來源:互聯網
上載者:User

標籤:script   att   prot   name   parent   span   訪問   取出   idt   

轉自:http://feitianbenyue.iteye.com/blog/2056357

最近在做一個項目, 架構上使用了 Nginx +tomcat 叢集, 且nginx下配置了SSL,tomcat no SSL,項目使用https協議

 

 


 

 

但是,明明是https url請求,發現 log裡面,

 

 

Xml代碼  
  1. 0428 15:55:55 INFO  (PaymentInterceptor.java:44) preHandle() - requestStringForLog:    {  
  2.         "request.getRequestURL():": "http://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6",  
  3.         "request.getMethod:": "GET",  
  4.         "_parameterMap":         {  
  5.             "id": ["212"],  
  6.             "s": ["a84485e0985afe97fffd7fd7741c93851d83a4f6"]  
  7.         }  
  8.     }  
 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上寫得很清楚:

 

getRequestURL():

 

Java代碼  
  1. Reconstructs the URL the client used to make the request.   
  2.   
  3. The returned URL contains a protocol, server name, port number, and server path,   
  4. but it does not include query string parameters.  

 

 

也就是說, getRequestURL() 輸出的是不帶query string的路經(含協議,連接埠,server path等資訊).

 



 

 

並且,還發現

 

Xml代碼  
  1. request.getScheme()  //總是 http,而不是實際的http或https  
  2. request.isSecure()  //總是false(因為總是http)  
  3. request.getRemoteAddr()  //總是 nginx 請求的 IP,而不是使用者的IP  
  4. request.getRequestURL()  //總是 nginx 請求的URL 而不是使用者實際請求的 URL  
  5. response.sendRedirect( 相對url )  //總是重新導向到 http 上 (因為認為當前是 http 請求)  

 

 

查閱了一些資料,找到瞭解決方案:

 

解決方案很簡單,只需要分別配置一下 Nginx 和 Tomcat 就好了,而不用改程式。

 

配置 Nginx 的轉寄選項:

  

Xml代碼  
  1. proxy_set_header       Host $host;  
  2. proxy_set_header  X-Real-IP  $remote_addr;  
  3. proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  
  4. proxy_set_header X-Forwarded-Proto  $scheme;  
  

proxy_set_header X-Forwarded-Proto $scheme;

 

 

配置Tomcat server.xml 的 Engine 模組下配置一個 Valve:

Xml代碼  
  1. <Valve className="org.apache.catalina.valves.RemoteIpValve"  
  2. remoteIpHeader="X-Forwarded-For"  
  3. protocolHeader="X-Forwarded-Proto"  
  4. 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

 

Xml代碼  
  1. 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").   
  2.    
  3. 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

 

Java代碼  
  1. if (protocolHeader != null) {  
  2.     String protocolHeaderValue = request.getHeader(protocolHeader);  
  3.     if (protocolHeaderValue == null) {  
  4.         // don‘t modify the secure,scheme and serverPort attributes  
  5.         // of the request  
  6.     } else if (protocolHeaderHttpsValue.equalsIgnoreCase(protocolHeaderValue)) {  
  7.         request.setSecure(true);  
  8.         // use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0  
  9.         request.getCoyoteRequest().scheme().setString("https");  
  10.           
  11.         request.setServerPort(httpsServerPort);  
  12.     } else {  
  13.         request.setSecure(false);  
  14.         // use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0  
  15.         request.getCoyoteRequest().scheme().setString("http");  
  16.           
  17.         request.setServerPort(httpServerPort);  
  18.     }  
  19. }  

 

  

 

參考: 

 

SSL認證與Https應用部署小結

http://han.guokai.blog.163.com/blog/static/136718271201211631456811/

Nginx SSL+tomcat叢集,request.getScheme() 取到https正確的協議

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.