2011-12-06 14:45:20 +00:00
|
|
|
Capistrano::Configuration.instance(:must_exist).load do
|
|
|
|
namespace :deploy do
|
|
|
|
namespace :nginx do
|
|
|
|
set :nginx_init_path, "/etc/init.d/nginx"
|
2011-12-06 18:35:19 +00:00
|
|
|
set(:nginx_config_path) { "/etc/nginx/conf.d/#{fetch :application}.conf" }
|
2011-12-06 14:45:20 +00:00
|
|
|
|
|
|
|
desc "Generate Nginx configuration"
|
2014-01-21 04:51:49 +00:00
|
|
|
task :generate_configuration, roles: :web, except: { no_release: true } do
|
2011-12-06 14:45:20 +00:00
|
|
|
config = %Q{
|
|
|
|
upstream #{application}_backend {
|
2011-12-14 12:34:44 +00:00
|
|
|
# server 127.0.0.1:#{unicorn_port rescue 8080};
|
2011-12-13 21:48:25 +00:00
|
|
|
server unix:/tmp/#{fetch :application}_unicorn.sock;
|
2011-12-06 14:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
listen 80;
|
|
|
|
server_name #{domain};
|
2011-12-06 18:35:19 +00:00
|
|
|
root #{fetch :current_path}/public;
|
2011-12-06 14:45:20 +00:00
|
|
|
|
|
|
|
location /downloads {
|
|
|
|
autoindex on;
|
|
|
|
}
|
|
|
|
|
|
|
|
try_files $uri/index.html $uri.html $uri @myapp;
|
|
|
|
|
|
|
|
location @myapp {
|
|
|
|
proxy_pass http://#{application}_backend;
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
proxy_set_header X-Forwarded-For $remote_addr;
|
|
|
|
proxy_read_timeout 300;
|
|
|
|
proxy_send_timeout 180;
|
|
|
|
proxy_redirect off;
|
|
|
|
}
|
|
|
|
|
2011-12-06 18:35:19 +00:00
|
|
|
access_log #{fetch :shared_path}/log/nginx.access.log;
|
|
|
|
error_log #{fetch :shared_path}/log/nginx.error.log;
|
2011-12-06 14:45:20 +00:00
|
|
|
|
|
|
|
error_page 500 502 503 504 /500.html;
|
|
|
|
location = /500.html {
|
2011-12-06 18:35:19 +00:00
|
|
|
root #{fetch :current_path}/public;
|
2011-12-06 14:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# puts config
|
2011-12-06 18:35:19 +00:00
|
|
|
puts "Write nginx config to #{fetch :nginx_config_path}"
|
2011-12-06 14:45:20 +00:00
|
|
|
put config, '/tmp/nginx.conf'
|
2011-12-06 18:35:19 +00:00
|
|
|
sudo "mv /tmp/nginx.conf #{fetch :nginx_config_path}"
|
2011-12-06 14:45:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "Start nginx web server"
|
2014-01-21 04:51:49 +00:00
|
|
|
task :start, roles: :web, except: { no_release: true } do
|
2011-12-06 14:45:20 +00:00
|
|
|
sudo "#{fetch :nginx_init_path} start"
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Stop nginx web server"
|
2014-01-21 04:51:49 +00:00
|
|
|
task :stop, roles: :web, except: { no_release: true } do
|
2011-12-06 14:45:20 +00:00
|
|
|
sudo "#{fetch :nginx_init_path} stop"
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Restart nginx web server"
|
2014-01-21 04:51:49 +00:00
|
|
|
task :restart, roles: :web, except: { no_release: true } do
|
2011-12-06 14:45:20 +00:00
|
|
|
sudo "#{fetch :nginx_init_path} restart"
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Resload nginx web server"
|
2014-01-21 04:51:49 +00:00
|
|
|
task :reload, roles: :web, except: { no_release: true } do
|
2011-12-06 14:45:20 +00:00
|
|
|
sudo "#{fetch :nginx_init_path} reload"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|