5

我配置了一个可伸缩的EB(Elasticbeanstalk)rails(puma)实例。我已通过ACM(亚马逊证书管理器)申请https并将其应用到我的负载均衡器。现在我的网站已启用HTTPS。但我如何强制重定向到https?我在网上尝试了一些解决方案,建议通过.ebextensions手动创建一个nginx配置设置,我不知道从哪里获得ACM的证书?(我认为现在对于ACM是不可能的? )。我如何强制使用HTTPS?使用来自ACM的证书强制使用elasticbeanstalk中的https

+0

我跟着这个http://msnider.github.io/blog/2013/12/06/force-https-slash-ssl-on-amazon - 弹性beanstalk /和它的工作。您可能必须手动重新启动服务器才能正常工作?或者只是上传和部署。我还隐约记得必须将我的负载均衡器别名到我的域以获得我的证书才能工作,但这可能是因为我购买了扩展验证证书。 – vath

+0

似乎互联网无法就这个问题达成一个单一的,完整的和有效的解决方案。希望你能得到一些帮助[在我的帖子中](http://thehunk.blogspot.in/2017/11/how-to-force-redirect-http-to-https-in.html)。最后,我不得不跳过这个谜团。 – ADTC

回答

4

目前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/nginx.conf包含/etc/nginx/conf.d/01-force-https.conf。请注意,如果稍后从.ebextensions删除相应的文件,eb deploy将不会删除服务器上的文件。另外,我发现在调试以下有用通过eb ssh

sudo service nginx configtest 
sudo service nginx restart 
+0

谢谢!很好的答案 – thomasstephn

相关问题