加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用来自ACM的证书在elasticbeanstalk中强制ht

发布时间:2020-12-17 03:18:41 所属栏目:百科 来源:网络整理
导读:我已经配置了可扩展的EB(Elasticbeanstalk)rails(puma)实例.我已通过ACM(亚马逊证书管理器)申请https并将其应用于我的负载均衡器.现在为我的网站启用了HTTPS.但是如何强制重定向到https?我已经尝试了一些在线解决方案,建议通过.ebextensions手动进行nginx配
我已经配置了可扩展的EB(Elasticbeanstalk)rails(puma)实例.我已通过ACM(亚马逊证书管理器)申请https并将其应用于我的负载均衡器.现在为我的网站启用了HTTPS.但是如何强制重定向到https?我已经尝试了一些在线解决方案,建议通过.ebextensions手动进行nginx配置设置,我不知道从哪里获得ACM的证书?(我假设现在ACM无法做到这一点? ).如何强制HTTPS?

解决方法

当前的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 repo或app bundle和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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读