Http to Https AWS Elasticbeanstalk

时间:2018-03-27 12:25:08

标签: spring amazon-web-services redirect https ebextensions

我正在使用AWS Elasticbeanstalk作为我的Spring MVC Web应用程序。我想将所有请求重定向到https。我试着遵循这个How to force https on elastic beanstalk?,但这对我不起作用。此代码重定向到https但我的应用程序无效。它显示“此页面无效”。代码供您参考

<VirtualHost *:80>
  RewriteEngine on
  RewriteCond %{HTTP:X-Forwarded-Proto} =http
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  <Proxy *>
    Order Allow,Deny
    Allow from all
  </Proxy>
  ProxyPass / http://localhost:8080/ retry=0
  ProxyPassReverse / http://localhost:8080/
  ProxyPreserveHost on

  ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

假设您已经在使用HTTPS访问过您的网站时已经测试过HTTPS工作正常。如果没有,您可以添加此文件.ebextensions/loadbalancer-terminatehttps.config,其内容如下:

option_settings:
  aws:elb:listener:443:
    ListenerProtocol: HTTPS
    SSLCertificateId: arn:aws:acm:us-west-2:<your-account-id>:certificate/<certificate-arn-on-aws-acm>
    InstancePort: 80
    InstanceProtocol: HTTP

剩下的就是配置Apache配置实例,将使用HTTP访问您网站的客户端重定向到HTTPS,将下面的代码添加到新文件.ebextensions/001_ssl_rewrite.config

Apache 2.4 +

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>

Apache 2.2.X

files:
    "/etc/httpd/conf.d/ssl_rewrite.conf":
        mode: "000644"
        owner: root
        group: root
        content: |
            LoadModule rewrite_module modules/mod_rewrite.so
            RewriteEngine On
            # This will enable the Rewrite capabilities
            RewriteCond %{HTTPS} !=on
            # This checks to make sure the connection is not already HTTPS
            RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

您可以从here

查看Elastic Beanstalk上安装的Apache

有关详情,请阅读以下两个答案:https://stackoverflow.com/a/38751749/1772245https://stackoverflow.com/a/40670047/1772245