【问题标题】:Redirect http to https on Elastic Beanstalk Linux Tomcat 8在 Elastic Beanstalk Linux Tomcat 8 上将 http 重定向到 https
【发布时间】:2017-02-27 01:09:21
【问题描述】:

在过去的几天里,我一直在尝试强制我的应用程序将对我的域的非 https 调用转发到 https 调用。

我有一个配置了 64 位 Amazon Linux 2016.03 v2.2.0 运行 Tomcat 8 Java 8 的 Web 服务器弹性 beanstalk。我在我的应用程序中创建了一个名为 .ebextensions 的文件夹,其中包含一个名为 ssl_rewrite.config 的文件。该文件包含以下内容:

files:
  "/etc/httpd/conf.d/ssl_rewrite.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      RewriteEngine On
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

它曾经包含这个 taken from this example,但我收到了 if 语句的错误:

files:
  "/etc/httpd/conf.d/ssl_rewrite.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
        RewriteEngine On
        <If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
        </If>

无论如何,当我尝试部署我的应用时,它失败了,我收到了这个错误:

Application update failed at 2016-10-17T01:33:00Z with exit status 1 and error: Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/03_configure_proxy.sh failed.

Executing: /opt/elasticbeanstalk/bin/log-conf -n httpd -l'/var/log/httpd/*'

Executing: /usr/sbin/apachectl -t -f /var/elasticbeanstalk/staging/httpd/conf/httpd.conf
Syntax error on line 1 of /etc/httpd/conf.d/ssl_rewrite.conf:
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Failed to execute '/usr/sbin/apachectl -t -f /var/elasticbeanstalk/staging/httpd/conf/httpd.conf'
Failed to execute '/usr/sbin/apachectl -t -f /var/elasticbeanstalk/staging/httpd/conf/httpd.conf'.

我已经进入 httpd.conf 文件并添加了这一行:

LoadModule rewrite_module modules/mod_rewrite.so

关于我做错了什么的任何提示或想法?谢谢!

【问题讨论】:

    标签: amazon-web-services ssl mod-rewrite amazon-elastic-beanstalk amazon-elb


    【解决方案1】:

    对于那些挣扎了一段时间的人,我找到了一个包含所有 AWS 配置的 GitHub(来自 AWS 团队),下面的示例适用于 Apache 2.2 的 HTTP>HTTPS 重定向。 (有关 Apache 2.4 和 Nginx 的配置,请参阅下面的链接)。

    Apache 2.2

    1. 在应用的根目录中创建一个文件: YOUR_PROJECT_ROOT/.ebextensions/httpd/conf.d/elasticbeanstalk.conf (如果使用 IntelliJ / Java,请确保将其添加到最终的 .WAR 工件中)

    2. 添加以下行以在虚拟主机中启用重定向:

      <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 *>
              Order deny,allow
              Allow from all
          </Proxy>
      
          ProxyPass / http://localhost:8080/ retry=0
          ProxyPassReverse / http://localhost:8080/
          ProxyPreserveHost on
      
          ErrorLog /var/log/httpd/elasticbeanstalk-error_log
      </VirtualHost>
      

    有关 Apache 2.4 和 Nginx 的更多示例,请访问此 GitHub 存储库:

    https://github.com/awsdocs/elastic-beanstalk-samples/tree/master/configuration-files/aws-provided/security-configuration/https-redirect/java-tomcat

    此外,还有更多有用的配置和示例可用。

    问候

    【讨论】:

      【解决方案2】:

      我终于可以和我一起工作了:

      LoadModule rewrite_module modules/mod_rewrite.so
      RewriteEngine On
      
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
      

      此外,我发现在我的 .ebextensions/httpd/conf.d 文件夹中放置一个 ssl_rewrite.conf 并让 AWS 脚本处理其余部分更容易。

      如果使用虚拟主机,还请阅读http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#vhosts。 Mod_rewrite 配置不是由虚拟主机从主服务器上下文继承的,因此在每个虚拟主机配置中也需要以下内容。

      RewriteEngine On
      RewriteOptions Inherit
      

      在运行 Tomcat 7 Java 7 的 64 位 Amazon Linux 2016.09 v2.4.0 上测试。

      【讨论】:

      • 对我来说答案的关键是在Tomcat8上的.ebextensions/httpd/conf.d文件夹中创建conf文件
      猜你喜欢
      • 2018-01-07
      • 2016-06-19
      • 2013-03-30
      • 2018-01-01
      • 2019-09-21
      • 2021-02-23
      • 2018-02-16
      • 2018-09-30
      • 2017-02-04
      相关资源
      最近更新 更多