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

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

1
2
3
4
5
6
7
8
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():

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

并且,还发现

1
2
3
4
5
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 的转发选项:

1
2
3
4
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:

1
2
3
4
<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

1
2
3
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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");  
           
本文转自yunlielai51CTO博客,原文链接:http://blog.51cto.com/4925054/1949323,如需转载请自行联系原作者

Nginx SSL+tomcat集群配置SSL,ngnix配置SSL后js/css访问出现404相关推荐

  1. Nginx以及通过Nginx实现tomcat集群配置与负载均衡

    Nginx简介 启动,停止,和重新加载配置文件命令 Nginx功能 正向代理和反向代理的区别 反向代理 负载均衡 1.RR(默认) 2.权重 3.ip_hash 4.fair(第三方) 5.url_h ...

  2. Windows7 + Nginx + Memcached + Tomcat 集群 session 共享

    一,环境说明 操作系统是Windows7家庭版(有点不专业哦,呵呵!),JDK是1.6的版本, Tomcat是apache-tomcat-6.0.35-windows-x86,下载链接:http:// ...

  3. nginx+双tomcat集群负载均衡(一台机器)

    nginx简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...

  4. linux环境下通过nginx实现tomcat集群

    2019独角兽企业重金招聘Python工程师标准>>> linux环境下通过nginx实现tomcat集群 安装nginx之前需要pcre依赖和jvm-remote补丁 一.准备如下 ...

  5. Nginx+Memcached+Tomcat集群配置实践(Sticky Session)

    准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp [html] view plaincopy <%@ page language=& ...

  6. Nginx+Tomcat集群的安装与配置

    Nginx安装在ip为x.x.x.x的服务器上 Tomcat安装在ip为y.y.y.y的服务器上 nginx安装 第一步,安装编译工具及库文件,如果服务器上已经有了,可以不用再安装. 命令:yum - ...

  7. Nginx实现tomcat集群进行负载均衡

    一.背景 随着业务量和用户数量的激增,单一的tomcat部署应用已经无法满足性能需求,而且对于每次发布项目期间服务不可用的问题也凸显,既然出现了这个问题,那么我们本文就借助nginx来完美的解决这个问 ...

  8. 配置集群Nginx+Memcached+Tomcat集群配置

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下配置集群 1.   Nginx Nginx是通过将多个Web Server绑定到同一个IP地址下,以实现多个WebS ...

  9. nginx搭建tomcat集群

    下载最新版的nginx,我这里使用的是windows-1.19.8版本做测试,之前下载的低版本,试验了很久都没有成功,应该是配置写法有所更新. 接着,我通过springboot部署了两个tomcat, ...

最新文章

  1. SpringBoot与Spring的对比
  2. “旧城改造”的背后——银泰新零售阿里云解决方案(上)
  3. 字符串转换成java对象然后通过@RestController返回json对象
  4. 【机器学习】脑机接口利器:错误率仅3%
  5. 高效代码审查的十个经验
  6. golang 相互引用_Golang与C互用以及调用C的so动态库和a静态库
  7. php fwrite服务器上写不进去_使用PHP来简单的创建一个RPC服务
  8. 硅谷程序员跳槽排行榜:最爱竟不是 Google 苹果 Facebook!
  9. 《Android 源码设计模式解析与实战》— Android 书籍
  10. python连接mysql代码_Python连接MySQL及基本操作代码
  11. 六度空间 c语言 【详解】
  12. php 加密算法+接口安全技术
  13. Oracle什么时候创建索引最好
  14. java使用ajax请求下载excel响应结果显示乱码
  15. MThings连接移动OneNet物联网平台
  16. java中的NIO,BIO,AIO
  17. 代码随想录贪心算法——买卖股票的最佳时机含手续费
  18. HBuilderX的介绍与语法提示
  19. 西安电子科技大学数据结构考点大纲解析
  20. numpy 基本操作

热门文章

  1. python笔记之序列(list的基本使用和常用操作)
  2. 树莓派更换软件源提高下载速度
  3. #ifndef #define #endif ”防止头文件被重复包 .
  4. oracle中有类似split的方法么,Oracle 实现拆分列数据的split()方法
  5. php date 毫秒_swoole+PHP自动取消订单he还原库存
  6. swift 拖动按钮_Swift下使用UICollectionView 实现长按拖拽功能
  7. 绕过html标签,巧妙绕过WAF的XSS技巧
  8. 两个空间点直接距离投影公式_线积分与面积分(2):最初的公式
  9. python安装pyinstaller出现错误_pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)...
  10. 12位18Bb20计算公式c语言,C语言的一些简单题目,没有答案,哪位大神帮忙做一下!!!...