问题描述
项目采用springboot框架,内嵌tomcat容器。前端采用nginx反向代理,当部署了https以后出现的重定向(redirect)的问题。用nginx反向代理tomcat,然后把nginx配置为https访问,并且nginx与tomcat之间配置为普通的http协议,当后台代码定义时redirect,实际是重定向到了http下的地址,导致浏览器上无法访问非https的地址。
解决方案
配置nginx
由于对tomcat而言收到的是普通的http请求,因此当tomcat里的应用发生转向请求时将转向为http而非https,为此我们需要告诉tomcat已被https代理,方法是增加X-Forwared-Proto和X-Forwarded-Port两个HTTP头信息。
server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate /ssl/test.pem;
ssl_certificate_key /ssl/test.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
location / {
proxy_pass http://agent;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
配置tomcat
springboot配置参见Spring Boot Reference Guide-Use behind a front-end proxy server
基于spring-boot开发时只需在application.properties中进行配置。
该配置将指示tomcat从HTTP头信息中去获取协议信息(而非从HttpServletRequest中获取
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true
以下是经常被漏掉的配置,本人一开始就忘记配置导致不成功。因为我的nginx的机器IP不在默认允许IP列表里
Tomcat is also configured with a default regular expression that matches internal proxies that are to be trusted. By default, IP addresses in 10/8, 192.168/16, 169.254/16 and 127/8 are trusted. You can customize the valve’s configuration by adding an entry to application.properties, e.g.
server.tomcat.internal-proxies=172\\.16\\.\\d{1,3}\\.\\d{1,3}
此外,虽然我们的tomcat被nginx反向代理了,但仍可访问到其80端口。为此可在application.properties中增加一行:
server.address=127.0.0.1
这样一来其80端口就只能被本机访问了,其它机器访问不到。