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)

源码分析:

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);  
        }  
    }  
  • 如果没有配置protocolHeader 属性, 什么都不做.
  • 如果配置了protocolHeader,但是request.getHeader(protocolHeader)取出来的值是null,什么都不做
  • 如果配置了protocolHeader,但是request.getHeader(protocolHeader)取出来的值(忽略大小写)是 配置的protocolHeaderHttpsValue(默认https),scheme设置为https,端口设置 为 httpsServerPort
  • 其他设置为 http
相关推荐
Dxy12393102162 小时前
python连接minio报错:‘SSL routines‘, ‘ssl3_get_record‘, ‘wrong version number‘
开发语言·python·ssl
令狐少侠20114 小时前
docker基本操作 部署启动nginx
nginx·docker·容器
知南x4 小时前
【物联网视频监控系统----韦东山老师视频总结】(4)流媒体方案的实现之Nginx
物联网·nginx·音视频
成为你的宁宁9 小时前
【Zabbix运维监控实战(附图文教程):Nginx 服务可用性、连接请求状态、CPU 内存占用与 JVM(Jar 包 / Tomcat)全维度监控】
运维·jvm·nginx·zabbix
❀͜͡傀儡师10 小时前
基于shell脚本来检测SSL证书过期并发送通知到钉钉
网络协议·钉钉·ssl
Htojk11 小时前
openssl签发自签名证书的流程
网络协议·https·ssl
oMcLin11 小时前
如何在 Debian 11 上配置并调优 Nginx 与 Lua 脚本,提升高流量 API 网关的性能与安全性?
nginx·debian·lua
oMcLin11 小时前
如何在CentOS 8.4上配置并优化Nginx负载均衡,确保跨境电商平台的高并发请求稳定处理?
nginx·centos·负载均衡
遇见火星14 小时前
为MySQL配置SSL加密访问
mysql·adb·ssl
C_心欲无痕14 小时前
为什么前端项目部署需要 nginx 或 Apache?
前端·nginx·apache