使用opsworks nginx和ELB终止SSL时强制使用HTTPS

使用通过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}.在您的自定义食谱中:

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 (engine x) 是一个高性能的HTTP和反向代理服务,也是一...
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改l...
nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看...
一 、此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] ng...
###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "ima...