将 Opsworks 标准设置/配方用于通过 Unicorn/nginx 层提供的 Rails 应用程序。 SSL 在 Elastic Load Balancer 终止——因此从 ELB 到 rails 应用程序的流量始终是 http。到目前为止,一切都很好。我想对http://domain.com有任何要求被重定向到 https://domain.com
ELB 有两个监听器 - 一个使用端口 80,一个使用 443。
我知道如果我运行自己的 nginx,我可以设置一个重定向规则...但是,如果可能的话,我想留在 opsworks 做事的方式。
最佳答案
我认为在 OpsWorks 中执行此操作的唯一方法是创建一个修改 /etc/nginx/sites-available/#{application}
的自定义配方。在您的自定义食谱中:
somecookbook/recipes/nginx.rb
node[:deploy].each do |application, deploy|
service "nginx" do
supports :status => true
action :nothing
end
# Add HTTP => HTTPS redirect
template "/tmp/http_redirect.conf" do
source "nginx/http_redirect.conf.erb"
end
execute "redirect HTTP to HTTPS" do
cwd "/etc/nginx/sites-available"
command "cat #{application} /tmp/http_redirect.conf > /tmp/#{application}.conf && cat /tmp/#{application}.conf > #{application}"
notifies :restart, "service[nginx]", :immediately
end
end
end
然后在配置中:
somecookbook/templates/default/nginx/http_redirect.conf.erb
server {
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
关于nginx - 使用 opsworks nginx 和 ELB 终止 SSL 时强制使用 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342476/