当前位置: 首页 > news >正文

Nginx SSL+tomcat,使用request.getScheme() 取到https协议

架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https和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 的转发选项:

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 

RemoteIpValve (Apache Tomcat 6.0.53 API Documentation)

 

源码分析:

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);  }  }  
  • 如果没有配置protocolHeader 属性, 什么都不做.
  • 如果配置了protocolHeader,但是request.getHeader(protocolHeader)取出来的值是null,什么都不做
  • 如果配置了protocolHeader,但是request.getHeader(protocolHeader)取出来的值(忽略大小写)是 配置的protocolHeaderHttpsValue(默认https),scheme设置为https,端口设置 为 httpsServerPort
  • 其他设置为 http
http://www.lryc.cn/news/485045.html

相关文章:

  • Node.Js+Knex+MySQL增删改查的简单示例(Typescript)
  • 机器学习的概览
  • 方法论-WPS模型(高效沟通和决策分析的框架)
  • OpenTelemetry 赋能DevOps流程的可观测性革命
  • 子集选择——基于R语言实现(最优子集选择法、逐步回归法、Lasso回归法、交叉验证法)
  • Ubuntu24.04挂载磁盘
  • 使用机器学习优化数据库查询性能
  • 英伟达基于Mistral 7B开发新一代Embedding模型——NV-Embed-v2
  • HTML面试题(2)
  • 微服务day07
  • 芯原科技嵌入式面试题及参考答案
  • 二叉树Golang
  • 通过css的哪些方式可以实现隐藏页面上的元素?
  • 微信小程序 === 使用腾讯地图选点
  • Redis高可用-Cluster(集群)
  • Spring Boot编程训练系统:数据管理与存储
  • 报告解读 | 创意经济2024:如何在变革中抢占先机?
  • Flume1.9.0自定义Sink组件将数据发送至Mysql
  • 如何在 Ubuntu 24.04 上安装和配置 Fail2ban ?
  • uniapp如何i18n国际化
  • C++__day1
  • Emacs进阶之插入时间信息(一百六十三)
  • Java线程池:ThreadPoolExecutor原理解析
  • 二叉树、哈夫曼报文大全
  • NotePad++中安装XML Tools插件
  • 聊天服务器(7)数据模块
  • VS2022编译32位OpenCV
  • WP网站如何增加文章/页面的自定义模板
  • 【Linux网络编程】简单的UDP网络程序
  • LabVIEW中坐标排序与旋转 参见附件snippet程序