Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议

最近在做一个项目, 架构上使用了 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. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现nginxtomcat一起部署springboot项目并实现tomcat集群,需要进行以下步骤: 1. 部署springboot项目到tomcat上,并启动多个tomcat实例,这些实例需要在不同的端口上运行。 2. 配置nginx作为反向代理服务器,将所有请求转发到tomcat集群中的一个实例上。可以使用upstream模块来配置tomcat集群,如下所示: ```nginx http { upstream tomcat_cluster { server tomcat1_ip:tomcat1_port; server tomcat2_ip:tomcat2_port; server tomcat3_ip:tomcat3_port; } server { listen 80; server_name example.com; location / { proxy_pass http://tomcat_cluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } } ``` 在上面的配置中,tomcat_cluster是一个upstream实例,包含了所有tomcat实例的IP地址和端口号。nginx会将请求转发到tomcat_cluster中的一个实例上。 3. 配置session共享,以便在tomcat集群中的不同实例之间共享用户会话信息。可以使用memcached或redis等分布式缓存来实现session共享。 4. 配置负载均衡器,以便nginx可以根据不同的负载均衡算法来分配请求到不同的tomcat实例上。可以使用nginx自带的负载均衡模块或第三方模块,如nginx-upsync-module等。 5. 测试集群的可伸缩性和容错性,以确保tomcat集群可以在高负载和节点故障的情况下正常运行。 总结来说,要实现nginxtomcat一起部署springboot项目并实现tomcat集群,需要进行反向代理、session共享、负载均衡器等多方面的配置和测试。这样可以保证项目的高可用性和可伸缩性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值