我已经配置了一个可扩展的 EB(Elasticbeanstalk)rails(puma)实例。我已通过 ACM(Amazon Certificate Manager)申请了 https 并将其应用到我的负载均衡器。我的网站现已启用 HTTPS。但如何强制重定向到 https?我在网上尝试了许多解决方案,其中建议通过 .ebextensions 手动进行 nginx 配置设置,但我不确定从哪里获取 ACM 的证书?(我假设现在 ACM 不可能? )。如何强制使用 HTTPS?
当前的 AWS EB Rails 和 Node.js 设置均使用 nginx(如果您的 Web 服务器是 apache,请参阅这个答案),所以以下应该有效(改编自这个问题):
创建文件.ebextensions/01-force-https.config
(the .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
。注意eb deploy
如果您稍后从服务器中删除相应的文件,则不会删除服务器上的文件.ebextensions
。另外,我发现以下内容有助于调试eb ssh
:
sudo service nginx configtest
sudo service nginx restart
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)