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

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

  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():

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

  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:

  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

  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. 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. 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. request.setServerPort(httpsServerPort);
  11. } else {
  12. request.setSecure(false);
  13. // use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0
  14. request.getCoyoteRequest().scheme().setString("http");
  15. request.setServerPort(httpServerPort);
  16. }
  17. }

Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议的更多相关文章

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

    转自http://www.cnblogs.com/interdrp/p/4881785.html 最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,to ...

  2. request.getScheme() 取到https正确的协议(转载)

    最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https协议 但是,明明是https url请求,发现 log里 ...

  3. Docker Compose 一键部署Nginx代理Tomcat集群

    Docker Compose 一键部署Nginx代理Tomcat集群 目录结构 [root@localhost ~]# tree compose_nginx_tomcat/ compose_nginx ...

  4. Nginx+Memcached+Tomcat集群配置(MSM--win7 64bit)

    本次主要是在win7 64 上演示操作. web应用构建 Memcached安装配置启动 Tomcat配置 所需jar包 memcached-session-manager 序列化 contextxm ...

  5. nginx整合tomcat集群并做session共享----测试案例

    最近出于好奇心,研究了一下tomcat集群配置,并整合nginx,实现负载均衡,session共享,写篇记录,防止遗忘.---------菜鸡的自我修炼. 说明:博主采用一个web项目同时部署到两台t ...

  6. Ubuntu下基于Nginx实现Tomcat集群负载均衡

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   Nginx是一款HTTP和反向代理服务器,有关它的介绍可以到网上搜一下,很多很多,不再累述.这里,我们记录一下Nginx ...

  7. 使用Nginx实现Tomcat集群负载均衡

    概述 要解决的问题 环境准备以及问题解决思路 配置 测试 小结 一.概述 使用Nginx主要是来解决高并发情况下的负载均衡问题. 二.要解决的问题 1.最主要是负载均衡请求分发. 2.文件上传功能,只 ...

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

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

  9. 利用nginx搭建tomcat集群

    1.tomcat集群 利用nginx对请求进行分流,将请求平均的分给不同的tomcat去处理,减少单个tomcat的负载量,提高tomcat的响应速度. 2.创建多个tomcat服务器(同一个服务器上 ...

随机推荐

  1. C#获取H5页面上传图片代码

    基于上一篇的H5压缩上传图片,由于图片是以二进制字符流blob的形式传过来的,所以应该想将其转成bytes类型再进行转换 public void ProcessRequest(HttpContext ...

  2. [20170617]vim中调用sqlplus.txt

    [20170617]vim中调用sqlplus.txt --//以前写过一篇emacs下调用sqlplus的文章,一直想学emacs,受限制自己掌握vim,对学习它没有兴趣,原链接如下:--//htt ...

  3. 洗礼灵魂,修炼python(48)--巩固篇—模块

    模块 其实前面都说过的,不过还是系统的再说一次,相信学到这,大部分都被搞忘了吧,所以再提一下,也为后面的博文做铺垫 1.什么是模块 在程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越 ...

  4. Excel两列查找重复值

    判断A列的值在B列是否存在,如果存在则输出“yes” 在C1单元格使用如下公式: =IF(COUNTIF(A:A,B2)>0,"yes","")

  5. c# 百度地图api APP SN校验失败

    在使用c#调用百度地图Web服务api遇到的签名(sn校验)问题,在此记录一下,(ip白名单校验的请忽略) 1.首先获取ak与sk,这个两个东西可以从控制台中获取到 2.在这个地址:sn签名算法,里面 ...

  6. 禁用selinux

    查看selinux状态: [root@VM000000518 upload]# getenforce Enforcing 禁用: [root@VM000000518 upload]# setenfor ...

  7. 设计 MySQL 数据表的时候一般都有一列为自增 ID,这样设计原因是什么,有什么好处?

    知乎采集: MyISAM/InnoDB默认用B-Tree索引(可理解为"排好序的快速查找结构"). InnoDB中,主索引文件上直接存放该行数据,称为聚簇索引.次索引指向对主键的引 ...

  8. <20180927>新开一篇章记录常用到的IT名词

    (一)有些名词在市场化的今天用的很多,和IT的名词重叠和易混淆, 有些缩写根本连外国人都很少用, 国人却用的津津乐道.这篇是专门用来记录的. 1.1 SMB , 这个在IT领域用的很多, 阐述的是 “ ...

  9. UOJ 【UR #5】怎样跑得更快

    [UOJ#62]怎样跑得更快 题面 这个题让人有高斯消元的冲动,但肯定是不行的. 这个题算是莫比乌斯反演的一个非常巧妙的应用(不看题解不会做). 套路1: 因为\(b(i)\)能表达成一系列\(x(i ...

  10. RestTemplate 服务名请求

    @loadBalance注解修饰的restTemplate才能实现服务名的调用,没有修饰的restTemplate是没有该功能的. @loadBalance是Netflix的ribbon中的一个负载均 ...