【问题标题】:Forcing https in elasticbeanstalk with certificate from ACM使用来自 ACM 的证书强制在 elasticbeanstalk 中使用 https
【发布时间】:2016-09-22 12:24:15
【问题描述】:
我已经配置了一个可扩展的 EB(Elasticbeanstalk) rails(puma) 实例。我已经通过 ACM(Amazon Certificate Manager) 申请了 https 并将其应用于我的负载均衡器。我的网站现在启用了 HTTPS。但是如何强制重定向到 https?我在网上尝试了许多解决方案,建议通过 .ebextensions 手动进行 nginx 配置设置,但我不确定从哪里获得 ACM 的证书?(我假设现在 ACM 无法做到这一点? )。如何强制使用 HTTPS?
【问题讨论】:
标签:
ruby-on-rails
amazon-web-services
nginx
ssl-certificate
amazon-elastic-beanstalk
【解决方案1】:
当前的 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/conf.d/01-force-https.conf,它会自动包含在/etc/nginx/nginx.conf 中。请注意,如果您稍后从.ebextensions 中删除相应的文件,eb deploy 不会删除服务器上的文件。另外,我发现以下内容对通过eb ssh 进行调试很有帮助:
sudo service nginx configtest
sudo service nginx restart