SpringBoot+Nginx+Https下redirect问题
来源:互联网 发布:大数据四大特征 编辑:程序博客网 时间:2024/06/13 04:49
问题描述
项目采用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 80; listen 443 ssl; server_name localhost; ssl_certificate server.crt; ssl_certificate_key server.key; location / { proxy_pass http://localhost:8080; 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-forserver.tomcat.protocol_header=x-forwarded-protoserver.tomcat.port-header=X-Forwarded-Portserver.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=192\\.168\\.\\d{1,3}\\.\\d{1,3}
此外,虽然我们的tomcat被nginx反向代理了,但仍可访问到其8080端口。为此可在application.properties中增加一行:
server.address=127.0.0.1
这样一来其8080端口就只能被本机访问了,其它机器访问不到。
- SpringBoot+Nginx+Https下redirect问题
- nginx下redirect问题
- 解决nginx https代理tomcat redirect问题
- 解决nginx https代理tomcat redirect问题
- nginx SSl redirect问题
- web整站https环境下使用(spring mvc)redirect重定向到http协议问题
- 反向代理https后redirect出现问题
- Springboot项目使用Nginx 并配置 HTTPS
- nginx下开启https
- nginx下配置https
- Passenger,nginx and SSL(http redirect to https)
- Redirect all HTTP requests to HTTPS with Nginx
- jboss容器,web整站https环境下使用(spring mvc)redirect重定向到http协议问题
- nginx 代理https后,spring mvc web应用redirect https变成http
- Ubuntu下配置Nginx HTTPS
- Nginx下的https配置
- nginx下http转https
- windwos下Nginx配置https
- codeforces 280c 概率dp + dfs
- python 元祖方法
- mysql cluster双机集群
- PPP过程
- 万用表的使用
- SpringBoot+Nginx+Https下redirect问题
- JavaScript 定时器和延迟器
- 这是我的CSDN新起点
- web项目和Java项目的区别
- mysql cluster rpm安装包的选择 安装 配置 测试
- 各大公司Java面试题分类总结
- Activity 四种启动模式介绍
- I2C器件之PCF8574TS调试记录
- Android 之Fragment