使用来自ACM的证书强制在elasticbeanstalk中使用https

Ara*_*ind 5 ruby-on-rails nginx ssl-certificate amazon-web-services amazon-elastic-beanstalk

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

mb2*_*b21 5

当前的AWS EB Rails和Node.js设置都使用nginx(如果您的Web服务器是apache,请参阅此答案),因此以下内容应该有效(根据此问题改编):

使用以下内容创建文件.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;
      }
Run Code Online (Sandbox Code Playgroud)

如果你的环境是负载平衡的,你遗憾的是不能简单地添加到现有的配置,但需要用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
Run Code Online (Sandbox Code Playgroud)

然后将其添加到您的git repo或app bundle中eb deploy.这会创建/etc/nginx/conf.d/01-force-https.conf自动包含的内容/etc/nginx/nginx.conf.请注意,eb deploy如果稍后从中删除相应的文件,则不会删除服务器上的文件.ebextensions.另外,我发现以下内容有助于调试eb ssh:

sudo service nginx configtest
sudo service nginx restart
Run Code Online (Sandbox Code Playgroud)