我提供了一个可扩展的EB(Elasticbeanstalk)rails(puma)实例。 我通过ACM(亚马逊证书pipe理器)申请了https并将其应用到我的负载均衡器。 现在我的网站启用了HTTPS。 但是,我如何强制redirect到https? 我已经尝试了一些在线的解决scheme,build议通过.ebextensions手动创build一个nginxconfiguration设置,我不确定从ACM获取证书的位置(我认为现在对于ACM是不可能的? )。 我如何强制HTTPS?
当前的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; }
如果你的环境是负载平衡的,那么不幸的是,你不能简单地添加到现有的配置中,而是需要使用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
。 请注意,如果稍后从.ebextensions
删除相应文件,则eb deploy
不会删除服务器上的文件。 此外,我发现以下有用的调试通过eb ssh
:
sudo service nginx configtest sudo service nginx restart