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

标签 nginx aws-opsworks

将 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/

相关文章:

ssl - 服务器在使用 https [nginx] 时显示 404

mysql - 在 OpsWorks 中部署时出现问题

nginx - 带有 nginx 的 FastAPI 不提供 HTTPS 中的静态文件

Dockerized Nginx : Can static assets be updated without stopping the container?

node.js - 在 aws opsworks 上升级 node.js

chef-infra - 将 ssh_authorized_keys 外部说明书与 Amazon OpsWorks 结合使用

ruby-on-rails - 在 ubuntu 14.04 上安装 rmagick gem

ruby - 在 Amazon OpsWorks 上使用 Ruby 2.0

wordpress - 特定路径和某些文件类型的 Nginx 位置 block

python - Tornado nginx websockets 握手 400 错误