Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议详解
来源:互联网 发布:仙剑奇侠传四结局 知乎 编辑:程序博客网 时间:2024/06/16 18:47
欢迎扫码加入Java高知群交流
公司之前用的是http,但是出于苹果app审核和服务器安全性问题,要改为https,我们公司用的是沃通的ssl,按照沃通的官方文档提供的步骤完成服务器的配置。 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat 没有配置SSL,项目使用https协议。
配置成功后明明是https url请求,发现 log里面,tomcat获取scheme的时候,一直是http,而不是想像中的https
0415 16:01:10 INFO (PaymentInterceptor.java:44) preHandle() - requestStringForLog: { "request.getRequestURL():": "http://m.xxx.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6", "request.getMethod:": "GET", "_parameterMap": { "id": ["212"], "s": ["a84485e0985afe97fffd7fd7741c93851d83a4f6"] } }
request.getRequestURL() 输出出来的 一直是
http://m.xxx.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6
但是浏览器中的URL却是
https://m.xxx.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6
下面我们进一步研究发现,java API上写得很清楚:
getRequestURL():
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.
并且还发现
request.getScheme() //总是 http,而不是实际的http或httpsrequest.isSecure() //总是false(因为总是http)request.getRemoteAddr() //总是 nginx 请求的 IP,而不是用户的IPrequest.getRequestURL() //总是 nginx 请求的URL 而不是用户实际请求的 URLresponse.sendRedirect( 相对url ) //总是重定向到 http 上 (因为认为当前是 http 请求)
那么解决方案有没有呢,答案是肯定的,其实解决方法非常的简单,只需要分别配置一下 Nginx 和 Tomcat 就好了,而不用从程序代码上修改。
1.配置nginx的转发项,配置文件为proxy.conf,内容如下:
proxy_connect_timeout 300s;proxy_send_timeout 900;proxy_read_timeout 900;proxy_buffer_size 32k;proxy_buffers 4 64k;proxy_busy_buffers_size 128k;proxy_redirect off;proxy_hide_header Vary;proxy_set_header Accept-Encoding '';proxy_set_header Referer $http_referer;proxy_set_header Cookie $http_cookie;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;起到了关键性的作用。
2.配置tomcat,配置文件为server.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE server-xml [<!ENTITY vhost-localhost SYSTEM "file:///usr/local/tomcat-interface/conf/vhost/localhost.xml">]><Server port="8006" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/> <Listener className="org.apache.catalina.core.AprLifecycleListener"/> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" maxThreads="1000" minSpareThreads="20" acceptCount="1000" debug="0" disableUploadTimeout="true" useBodyEncodingForURI="true" enableLookups="false" URIEncoding="UTF-8" /> <Engine name="Catalina" defaultHost="localhost"> <Valve className="org.apache.catalina.valves.RemoteIpValve"remoteIpHeader="X-Forwarded-For"protocolHeader="X-Forwarded-Proto"protocolHeaderHttpsValue="https"/> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> &vhost-localhost; </Engine> </Service></Server>
其中关键的语句为:
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https"/>
配置双方的 X-Forwarded-Proto 就是为了正确地识别实际用户发出的协议是 http 还是 https。
配置完成之后以下的请求访问
request.getScheme() 、request.isSecure() 、request.getRemoteAddr()、request.getRequestURL() 、response.sendRedirect( 相对url )
就都变为正确的结果了,就像用户在直接访问 Tomcat 一样。
欢迎扫码加入Java高知群交流
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议详解
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
- nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议
- nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议
- Lvs配置Tomcat集群 Https协议(SSL)
- 【安全】 详解 Nginx + Tomcat HTTPS/SSL 配置
- 详解 Nginx + Tomcat HTTPS/SSL 配置方法
- 详解 Nginx + Tomcat HTTPS/SSL 配置方法
- 详解 Nginx + Tomcat HTTPS/SSL 配置
- 详解 Nginx + Tomcat HTTPS/SSL 配置
- 详解 Nginx + Tomcat HTTPS/SSL 配置方法
- 详解 Nginx + Tomcat HTTPS/SSL 配置
- Nginx SSL+Tomcat https
- java jsonpath 修改指定路径的值
- iOS单元测试
- Java简单聊天室第一个版本
- CPP复习笔记 3
- 【数据结构】求K阶斐波那契数列
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议详解
- 滑雪
- {小结}2017.04.15【NOIP2017提高组】模拟赛B组
- android/support/v4/text/TextUtilsCompat.class异常
- hadoop集群安装
- 腾讯课堂IMWeb小白(Day1)
- 将用户输入的字母进行大小写转换
- DOS常用命令
- 树套树-线段树套平衡树