首页 文章

Spring Boot和ELB - 如何使负载均衡器将http重定向到https?

提问于
浏览
0

我已经通过Elastic Beanstalk部署了一个Spring Boot应用程序 . 我正在使用负载均衡器,所以这是流程(据我所知):

Internet /浏览器请求--- HTTPS --->负载均衡器--- HTTP ---> Spring Boot App Server

基本上,SSL终止于负载均衡器,而app服务器只处理普通的旧HTTP .

但是对于来自浏览器的HTTP请求,我希望负载均衡器自动重定向到HTTPS .

关于这个问题有几个问题:

Spring Boot with Embedded Tomcat behind AWS ELB - HTTPS redirect
How to redirect automatically to https with Spring Boot
Spring Boot redirect HTTP to HTTPS

但这些问题的答案都没有对我有意义 . 也许我误解了,但所有答案基本上都使Spring Boot应用程序只有服务器HTTPS请求(例如使用 http.requiresChannel().anyRequest().requiresSecure() 时) .

但是,这与流程相反,因为我完全可以使用SSL终止于负载均衡器和Spring Boot应用服务器来处理HTTP . 因此,如果我在 spring 启动级别需要SSL,那么我需要进行端到端的SSL连接,这对我的应用程序来说并不是真正需要的 .

我还使用了以下属性,这些属性似乎也没有帮助:

server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto

3 回答

  • 0

    在这个article的帮助下,我终于能够弄清楚如何在ELB环境中为Spring Boot应用程序执行此操作 .

    我必须在 src/main/webapp/.ebextensions/nginx/conf.d 中创建一个conf文件 . 我把它叫做myconf.conf .

    myconf.conf 中,我将此代码放入:

    server {
        listen  80;
        server_name www.my-site.com;
        if ($http_x_forwarded_proto != "https") {
            rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
        }
    }
    

    此外,请确保为负载均衡器打开HTTP和HTTPS侦听器 .
    此外,我的 Spring 季启动应用程序只打开HTTP,因为负载均衡器已经终止SSL .

  • 1

    AWS Load Balancers无法发送重定向 . 这不是他们的特色 . 您必须检查服务器端的 x-forwarded-proto 标头,如果 x-forwarded-protohttp 而不是 https ,则返回重定向 .

  • 0

    AWS Load balancer无法处理重定向 . 您可以通过服务器或使用Cloudfront发行版来完成 .

相关问题