nginx配置ssl后代理tomcat获取不到https

星期五, 2020-02-14 | Author: Lee | JAVA-and-J2EE | 2,580 views

1.前置使用nginx做了反向代理

2.服务有tomcat提供,在tomcat里有获取getScheme总是http而不是https,需要解决.

遇见的问题如下:

request.getScheme()  //总是 http,而不是实际的http或https  
request.isSecure()  //总是false(因为总是http)  
request.getRemoteAddr()  //总是 nginx 请求的 IP,而不是用户的IP  
request.getRequestURL()  //总是 nginx 请求的URL 而不是用户实际请求的 URL  
response.sendRedirect( 相对url )  //总是重定向到 http 上 (因为认为当前是 http 请求)

3.解决办法

nginx的转发配置

主要是:proxy_set_header X-Forwarded-Proto $scheme;

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;

tomcat启用配置:

配置Tomcat server.xml 的 Engine 模块下配置一个 Valve:

<Valve className="org.apache.catalina.valves.RemoteIpValve"  
remoteIpHeader="X-Forwarded-For"  
protocolHeader="X-Forwarded-Proto"  
protocolHeaderHttpsValue="https"/>

重启nginx及tomcat搞定

Tags: ,

文章作者: Lee

本文地址: https://www.pomelolee.com/2029.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta