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

简介:

最近在做一个项目, 架构上使用了 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等信息).

 

40354-20151015111013710-2140974852.png

并且,还发现

 

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 ,如需转载请自行联系原作者

相关文章
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
517 21
|
负载均衡 Java 应用服务中间件
Tomcat与Nginx的负载均衡与动静分离技巧
总的来说,Tomcat和Nginx各有各的优点,在负载均衡和动静分离这两方面它们都有很好的应用。灵活使用这两个工具能够让Web应用具有更好的扩展性和用户体验。
501 14
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
1088 60
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
1179 60
|
网络安全 开发工具 git
解决 Git 访问 GitHub 时的 SSL 错误
通过上述步骤,可以有效解决 Git 访问 GitHub 时的 SSL 错误。推荐优先更新 CA 证书和正确配置 Git 使用 CA 证书,避免禁用 SSL 验证。如果问题持续,可以切换到 SSH 方式访问 GitHub,确保连接的安全性和稳定性。希望这些内容对您的学习和工作有所帮助。
8259 4
|
安全 网络协议 应用服务中间件
内网ip申请SSL证书实现https访问
内网IP地址虽不能直接申请公网SSL证书,但可通过IP SSL证书保障数据安全。流程包括:确定固定内网IP,选择支持内网IP的CA,注册申请证书,生成CSR,验证IP所有权,下载部署证书至Web服务器,测试HTTPS访问,确保配置正确及证书有效。此方法适用于内网环境,提升数据传输安全性。
内网ip申请SSL证书实现https访问
|
存储 应用服务中间件 Linux
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
1236 4
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
5748 8
|
前端开发 Java 应用服务中间件
Tomcat和Nginx的资源路径映射
Tomcat和Nginx的资源路径映射
712 1
|
安全 应用服务中间件 Shell
nginx配置https的ssl证书和域名
nginx配置https的ssl证书和域名