使用通过Unicorn / Nginx图层提供服务的Rails应用程序的Opsworks标准设置/食谱. SSL在Elastic Load Balancer处终止-因此从ELB到rails应用程序的流量始终为http.到现在为止还挺好.我想将http://domain.com的任何请求重定向到https://domain.com
ELB有两个侦听器-一个带有端口80,另一个是443.
我知道,如果我正在运行自己的Nginx,则可以设置重定向规则…但是,如果可能的话,我希望保留在opsworks的处理方式中.
最佳答案
我认为在OpsWorks中执行此操作的唯一方法是创建自定义配方,以修改/ etc / Nginx / sites-available /#{application}.在您的自定义食谱中:
原文链接:https://www.f2er.com/nginx/532427.htmlsomecookbook / 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;
}