Skip to content

Instantly share code, notes, and snippets.

@SunnyMagadan
Last active August 19, 2020 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SunnyMagadan/c44e33662d27b85ea92b to your computer and use it in GitHub Desktop.
Save SunnyMagadan/c44e33662d27b85ea92b to your computer and use it in GitHub Desktop.
Unicorn + Nginx + RoR configs
upstream project-name {
server unix:/var/www/project-name/current/tmp/sockets/project-name.socket;
}
server {
listen 80;
server_name my-project-name-host.com;
root /var/www/project-name/current/public;
# individual nginx logs for this vhost
access_log /var/log/nginx/project-name_access.log;
error_log /var/log/nginx/project-name_error.log;
location / {
# serve static files from defined root folder;.
# @project-name is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html @project-name;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (project-name unicorn)
location @project-name {
proxy_redirect off;
# you need to change this to "https", if you set "ssl" directive to "on"
proxy_set_header X-FORWARDED_PROTO http;
proxy_set_header Host my-project-name-host.com:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://project-name;
}
}
namespace :unicorn do
desc 'Stop Unicorn'
task :stop do
on roles(:app) do
if test("[ -f #{fetch(:unicorn_pid)} ]")
execute :kill, capture(:cat, fetch(:unicorn_pid))
end
end
end
desc 'Start Unicorn'
task :start do
on roles(:app) do
within current_path do
with rails_env: fetch(:rails_env) do
execute :bundle, "exec unicorn -c #{fetch(:unicorn_conf)} -D"
end
end
end
end
desc 'Reload Unicorn without killing master process'
task :reload do
on roles(:app) do
if test("[ -f #{fetch(:unicorn_pid)} ]")
execute :kill, '-s USR2', capture(:cat, fetch(:unicorn_pid))
else
error 'Unicorn process not running'
end
end
end
desc 'Restart Unicorn'
task :restart
before :restart, 'unicorn:stop'
after :restart, 'unicorn:start'
end
app_dir = "/var/www/project-name/current/"
worker_processes 2
working_directory app_dir
preload_app true
timeout 1800
listen "#{app_dir}/tmp/sockets/project-name.socket"
pid "#{app_dir}/tmp/pids/unicorn.pid"
stderr_path "#{app_dir}/log/unicorn.stderr.log"
stdout_path "#{app_dir}/log/unicorn.stdout.log"
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment