`

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

阅读更多

最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https协议

 

 


 

 

但是,明明是https url请求,发现 log里面,

 

 

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上写得很清楚:

 

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等信息).

 



 

 

并且,还发现

 

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;
  

proxy_set_header X-Forwarded-Proto $scheme;

 

 

配置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);
                }
            }

 

  

 

参考: 

 

SSL证书与Https应用部署小结

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

  • 大小: 21.2 KB
  • 大小: 9.5 KB
4
0
分享到:
评论
9 楼 飞天奔月 2018-08-21  
zuxianghuang 写道
解决问题了,谢谢



不客气
8 楼 zuxianghuang 2018-06-05  
解决问题了,谢谢
7 楼 lihui_shine 2016-12-26  
能帮忙看下吗
6 楼 lihui_shine 2016-12-26  
你好,我按照你的配置了,还是不行
我的nginx配置如下
server {
        listen      18443 ssl;
        server_name  localhost;
       ssl on;
        # access_log  logs/httpshost.access.log  main;
        ssl_certificate      my.crt;
        ssl_certificate_key  my.key;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM;
        ssl_prefer_server_ciphers  on;
        location / {
            proxy_pass http://myserver;
            proxy_set_header Host $host;
            proxy_redirect http:// $scheme://;
            proxy_set_header X-Forwarded-Proto "https";
        }
    }


tomcat设置了
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>


这样设置了还是不行
tomcat6、nginx1.8、jdk1.7
5 楼 leo_soul 2016-03-03  
哦 我找到办法了,只用NGINX的反向代理转发即可,tomcat和代码可不做配置。
nginx的配置文件内,当前SSL的location模块中加入
proxy_redirect  http://xxx.xxxxx.com https://xxx.xxxxx.com;
作用是,当上游的应用服务器,如tomcat接收到http请求返回数据后,补上的是http协议头,需要将其替换为https,域名/IP和端口号,用该命令也是可以重新替换的。这样反馈到客户端的就是你定义好的URL
需要注意的是,如TOMCAT中不配置valve,则程序中通过request获取包括协议、客户端IP等将是nginx的数据。程序中如有需要,可从HTTP协议头信息中获取,因为如文中所示,NGINX已配置了
proxy_set_header   Host             $host:$server_port;
proxy_set_header   X-Real-IP        $remote_addr; 
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
这些信息都在HTTP头里,但request.getXXX()方法是直接从请求URL中获取信息的。
4 楼 leo_soul 2016-03-01  
同样问题,感谢你的共享。顺便问一下,如果我的端口不是443而是很多不同的端口,tomcat里能够动态获取吗?
3 楼 zweixiang 2016-03-01  
前端nginx https +tomcat http 非80端口配置方式​
Nginx增加以下配置
proxy_set_header Host $host:$server_port; 非80端口 ,用80端口时 不需要$server_port
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 name="Catalina" defaultHost="localhost">
      <Valve className="org.apache.catalina.valves.RemoteIpValve"
             remoteIpHeader="X-Forwarded-For"
             protocolHeader="X-Forwarded-Proto"
             protocolHeaderHttpsValue="https"  httpsServerPort="7001"/> 非80端口时,必须增加httpsServerPort配置,不然request.getServerPort()方法返回 443.
</Engine>
2 楼 飞天奔月 2016-02-03  
dokia 写道
你好!我看你上面request.getRequestURL(),返回的url里面host是具体的域名,但是我配置后本地运行,request.getRequestURL()返回的url里面host是nginx.conf配置里面upstream后面的字符串值,请问你是怎么配置的?



我们有配置


proxy_set_header   Host             $host:$server_port;
proxy_set_header   X-Real-IP        $remote_addr; 
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_502 http_504;


仅供参考
1 楼 dokia 2016-01-29  
你好!我看你上面request.getRequestURL(),返回的url里面host是具体的域名,但是我配置后本地运行,request.getRequestURL()返回的url里面host是nginx.conf配置里面upstream后面的字符串值,请问你是怎么配置的?

相关推荐

Global site tag (gtag.js) - Google Analytics