【问题标题】:Http to Https AWS ElasticbeanstalkHttp 到 Https AWS Elasticbeanstalk
【发布时间】:2018-09-05 20:33:26
【问题描述】:

我将 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>

【问题讨论】:

  • 您在使用负载平衡器吗?如果是,他们是否终止 SSL/TLS 连接?
  • 是的,我允许 443 并配置了我的 ssl 证书
  • 太好了,当您使用 https:// domain .com 访问 https 网站时,它工作正常吗?
  • 没有,一旦我使用了这个 https 和 http 都不起作用。但在此之前 Https 工作正常。唯一的问题是它不会自动重定向
  • 我注意到使用此代码后,安全侦听器端口已关闭,然后我尝试启用它,然后出现此错误。 “LoadBalancerHTTPSPort:您已经指定了 @deprecated(:default.aws:elb:loadbalancer:LoadBalancerHTTPSPort) 选项以及新 aws:elb:listener:443 命名空间中的一个选项。:default.aws:elb:loadbalancer:LoadBalancerHTTPSPort选项将被忽略。”

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


【解决方案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 config 以将使用 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

【讨论】:

  • 检查更新的答案,看起来 AWS Java Beanstalk 图像使用的是旧版本的 Apache。
  • 它没有重定向,当我点击 http 时它没有重定向到 https
  • 尝试“RewriteCond %{HTTP:X-Forwarded-Proto} =http”而不是“RewriteCond %{HTTPS} !=on”,您能否确认您没有收到任何“无效命令”日志中有 RewriteEngine'" 错误?
  • 还要确保您没有对您的 tomcat 进行任何其他更改。您也可以通过 ssh 进入实例并编辑文件 /etc/httpd/conf.d/ssl_rewrite.conf 并重新启动 apache 然后测试...等。如果您需要任何其他问题,请告诉我。
  • 添加该文件为我工作。我没有按照其他事情添加该文件完成工作
猜你喜欢
  • 2020-01-27
  • 2017-06-14
  • 2021-03-13
  • 2015-06-27
  • 2023-01-27
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 2014-10-20
相关资源
最近更新 更多