Skip to content

Instantly share code, notes, and snippets.

@johnantoni
Created August 2, 2012 15:48
Show Gist options
  • Save johnantoni/3238062 to your computer and use it in GitHub Desktop.
Save johnantoni/3238062 to your computer and use it in GitHub Desktop.
unicorn + nginx setup + ssl
unicorn.rb
-----------------------------------
application = "jarvis"
remote_user = "vagrant"
env = ENV["RAILS_ENV"] || "development"
RAILS_ROOT = File.join("/home", remote_user, application)
worker_processes 8
timeout 30
preload_app true
working_directory RAILS_ROOT
listen File.join(RAILS_ROOT, "tmp/unicorn.sock"), :backlog => 64
pid_path = File.join(RAILS_ROOT, "tmp/pids/unicorn.pid")
pid pid_path
stderr_path File.join(RAILS_ROOT, "log/unicorn-err.log")
stdout_path File.join(RAILS_ROOT, "log/unicorn-err.log")
before_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
old_pid_path = "#{pid_path}.oldbin"
if File.exists?(old_pid_path) && server.pid != old_pid_path
begin
Process.kill("QUIT", File.read(old_pid_path).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# worker processes http://devmull.net/articles/unicorn-resque-bluepill
# rails_env = ENV['RAILS_ENV'] || 'production'
# worker.user('app', 'app') if Process.euid == 0 && rails_env == 'production'
end
nginx
----------------------------------
upstream dev.jarvis.com {
server unix:/home/vagrant/jarvis/tmp/unicorn.sock fail_timeout=0;
}
server {
listen 443 default;
ssl on;
ssl_certificate /home/vagrant/ssl/server.cer;
ssl_certificate_key /home/vagrant/ssl/server.key;
server_name dev.jarvis.com;
root /home/vagrant/jarvis/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_redirect off;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://dev.jarvis.com;
}
error_page 502 503 /maintenance.html;
error_page 500 504 /500.html;
keepalive_timeout 5;
}
@felipeclopes
Copy link

Cool! Do you have your deployment version? I'm having headaches trying to make this ssl works with nginx + unicorn using rubber scripts. If you have your unicorn and nginx configs for me it would be great!

Tnx!

@martinstreicher
Copy link

I am looking for a Unicorn + nginx + http > https example.

@r00takaspin
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment