设为首页 收藏本站
查看: 1898|回复: 0

[经验分享] Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议

[复制链接]

尚未签到

发表于 2015-12-23 10:56:26 | 显示全部楼层 |阅读模式
DSC0000.png




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







Xml代码 DSC0001.gif DSC0002.png DSC0003.gif

  • 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"]
  • }
  • }

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观 DSC0004.gif ,API上写得很清楚:
  

  getRequestURL():
  




Java代码

  • 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.

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



DSC0005.png



并且,还发现





Xml代码

  • request.getScheme()  //总是 http,而不是实际的http或https
  • request.isSecure()  //总是false(因为总是http)
  • request.getRemoteAddr()  //总是 nginx 请求的 IP,而不是用户的IP
  • request.getRequestURL()  //总是 nginx 请求的URL 而不是用户实际请求的 URL
  • response.sendRedirect( 相对url )  //总是重定向到 http 上 (因为认为当前是 http 请求)

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 的转发选项:





Xml代码

  • 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       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:



Xml代码

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

<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





Xml代码

  • 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").

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





Java代码

  • 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://feitianbenyue.iyunv.com/blog/2056357

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-155169-1-1.html 上篇帖子: Zabbix 监控 Nginx 状态 下篇帖子: Nginx 前端及配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表