【问题标题】:Redirect Http Url to Https in Elastic beanstalk loadbalancer将 Http Url 重定向到 Elastic beanstalk 负载均衡器中的 Https
【发布时间】:2017-12-01 23:33:59
【问题描述】:

我的 Spring Boot 应用在负载均衡器后面的 tomcat EC2 实例上运行,该实例配置了 Https,内部使用 Http。

我想将 url 请求重定向到 HTTP 到 HTTPS。

我从 AWS Support 找到了这个 document

正如它所说,我需要使用以下配置来配置 Apache 后端

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} =http

RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

我的问题是在哪里添加这些配置?

另一个文件说我需要将 .ebextensions 目录添加到 webapp 目录并在那里放置配置。如果是这样,那么目录结构和配置文件格式是什么?

【问题讨论】:

    标签: amazon-web-services spring-boot amazon-ec2 https elastic-load-balancer


    【解决方案1】:

    找到解决方案在以下位置创建配置文件

    ~/workspace/server/
    |-- .ebextensions
    |   -- httpd
    |      -- conf.d
    |         -- abc.conf
    

    内容如下

    <VirtualHost *:80>
       LoadModule rewrite_module modules/mod_rewrite.so
    
       RewriteEngine On
       RewriteCond %{HTTP:X-Forwarded-Proto} !https
       RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
       RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
       <Proxy *>
         Require all granted
       </Proxy>
    
       ProxyPass / http://localhost:8080/ retry=0
       ProxyPassReverse / http://localhost:8080/
       ProxyPreserveHost on
    
       ErrorLog /var/log/httpd/elasticbeanstalk-error_log
    
    </VirtualHost>
    

    在较新的 Tomcat 安装中需要加载 mod_rewrite。

    来源:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html

    【讨论】:

      【解决方案2】:

      在您的项目的根目录中创建一个文件夹 .ebextensions(并确保它位于您要上传到 Elastic Beanstalk 的捆绑包的根目录中。像这样:

      ~/workspace/my-app/
      |-- .ebextensions
      |   -- httpd
      |      -- conf.d
      |         -- myconf.conf
      -- index.jsp
      

      myconf.conf

      RewriteEngine On
      RewriteCond %{HTTP:X-Forwarded-Proto} =http
      RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
      

      有一整篇关于自定义在 Elastic Beanstalk Tomcat 中运行的 Java 应用程序的文章:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-platform.html 查看更多信息。

      【讨论】:

      • 感谢回复,我去测试一下。
      • 它不工作。在日志中,我发现“无效命令 'RewriteEngine',可能拼写错误或由未包含在服务器配置中的模块定义”
      • 我将其标记为已接受,因为它正在回答有关目录结构的主要问题。有关完整答案,请查看下面的答案。 stackoverflow.com/questions/44805261/…
      猜你喜欢
      • 2016-11-26
      • 2019-07-25
      • 2018-01-21
      • 2017-09-28
      • 2014-06-18
      • 2020-10-11
      • 1970-01-01
      • 2014-08-27
      • 2015-03-18
      相关资源
      最近更新 更多