【问题标题】:Forcing https in elasticbeanstalk with certificate from ACM使用来自 ACM 的证书强制在 elasticbeanstalk 中使用 https
【发布时间】:2016-09-22 12:24:15
【问题描述】:

我已经配置了一个可扩展的 EB(Elasticbeanstalk) rails(puma) 实例。我已经通过 ACM(Amazon Certificate Manager) 申请了 https 并将其应用于我的负载均衡器。我的网站现在启用了 HTTPS。但是如何强制重定向到 https?我在网上尝试了许多解决方案,建议通过 .ebextensions 手动进行 nginx 配置设置,但我不确定从哪里获得 ACM 的证书?(我假设现在 ACM 无法做到这一点? )。如何强制使用 HTTPS?

【问题讨论】:

  • 我关注了这个msnider.github.io/blog/2013/12/06/…,它成功了。您可能必须手动重新启动服务器才能使其正常工作?或者只是上传和部署。我还隐约记得必须将负载均衡器别名为我的域才能使我的证书正常工作,但这可能是因为我购买了扩展验证证书。
  • 互联网似乎无法就这个问题的单一、完整和有效的解决方案达成一致。希望你能得到一些帮助here in my post。最后,我不得不费尽心思才想出这个。

标签: ruby-on-rails amazon-web-services nginx ssl-certificate amazon-elastic-beanstalk


【解决方案1】:

当前的 AWS EB Rails 和 Node.js 设置都使用 nginx(如果您的 Web 服务器是 apache,请参阅 this answer),因此以下应该可以工作(改编自 this question):

使用以下内容创建文件.ebextensions/01-force-https.config.config 很重要,而不是.conf)。

如果您的环境是单个实例:

files:
  "/etc/nginx/conf.d/01-force-https.conf":
    owner: root
    group: root
    mode: "000644"
    content: |
      server {
          listen 8080;
          return 301 https://$host$request_uri;
      }

如果您的环境是负载平衡的,很遗憾您不能简单地添加到现有配置中,而是需要使用 sed 对其进行修改:

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /opt/elasticbeanstalk/support/conf/webapp_healthd.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i '/listen 80;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /opt/elasticbeanstalk/support/conf/webapp_healthd.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

然后将其添加到您的 git 存储库或应用程序包和 eb deploy。这会创建/etc/nginx/conf.d/01-force-https.conf,它会自动包含在/etc/nginx/nginx.conf 中。请注意,如果您稍后从.ebextensions 中删除相应的文件,eb deploy 不会删除服务器上的文件。另外,我发现以下内容对通过eb ssh 进行调试很有帮助:

sudo service nginx configtest
sudo service nginx restart

【讨论】:

【解决方案2】:

AWS 在这里有一篇关于 HTTP 到 HTTPS 重定向的帮助文章: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html

它涵盖了 2 种主要方法,并包含相关脚本的链接,您可以使用这些脚本为您完成所有工作(他们在更新 Elastic Beanstalk 平台时维护这些脚本)。

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2017-08-28
    • 2020-07-04
    • 2021-02-22
    • 2018-07-09
    • 2020-01-20
    相关资源
    最近更新 更多