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

相關問題