SpringBoot+Nginx+Https下redirect问题

本文介绍如何解决在SpringBoot项目中部署HTTPS后,使用nginx反向代理时遇到的重定向问题。通过配置nginx及修改springboot的application.properties文件来确保重定向到正确的HTTPS地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

项目采用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-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=192\\.168\\.\\d{1,3}\\.\\d{1,3}

此外,虽然我们的tomcat被nginx反向代理了,但仍可访问到其8080端口。为此可在application.properties中增加一行:

server.address=127.0.0.1

这样一来其8080端口就只能被本机访问了,其它机器访问不到。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值