在 Elastic Beanstalk Linux Tomcat 8 上将 http 重定向到 https

Redirect http to https on Elastic Beanstalk Linux Tomcat 8

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

我有一个配置了 64 位 Amazon Linux 2016.03, v2.2.0 运行 Tomcat 8 Java 8 的 Web 服务器 elastic 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

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

我终于能够使用:

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

在 64 位亚马逊上测试 Linux 2016.09 v2.4.0 运行 Tomcat 7 Java 7.

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

阿帕奇 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

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

此致