Created
June 15, 2011 04:45
-
-
Save brentkirby/1026496 to your computer and use it in GitHub Desktop.
Unicorn / Nginx / RVM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RAILS_ROOT = ENV['RAILS_ROOT'] || File.expand_path(File.dirname(File.dirname(__FILE__))) | |
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm') | |
begin | |
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME'])) | |
rvm_lib_path = File.join(rvm_path, 'lib') | |
$LOAD_PATH.unshift rvm_lib_path | |
require 'rvm' | |
RVM.use_from_path! RAILS_ROOT | |
rescue LoadError | |
raise "RVM ruby lib is currently unavailable." | |
end | |
end | |
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__)) | |
require 'bundler/setup' | |
worker_processes 1 | |
working_directory RAILS_ROOT | |
preload_app true | |
timeout 30 | |
listen RAILS_ROOT + "/tmp/sockets/unicorn.sock", :backlog => 64 | |
pid RAILS_ROOT + "/tmp/pids/unicorn.pid" | |
stderr_path RAILS_ROOT + "/log/unicorn.stderr.log" | |
stdout_path RAILS_ROOT + "/log/unicorn.stdout.log" | |
before_fork do |server, worker| | |
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! | |
old_pid = RAILS_ROOT + '/tmp/pids/unicorn.pid.oldbin' | |
if File.exists?(old_pid) && server.pid != old_pid | |
begin | |
Process.kill("QUIT", File.read(old_pid).to_i) | |
rescue Errno::ENOENT, Errno::ESRCH | |
puts "Old master alerady dead" | |
end | |
end | |
end | |
after_fork do |server, worker| | |
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream unicorn { | |
server unix:/path/to/shared/sockets/unicorn.sock fail_timeout=0; | |
} | |
server { | |
listen 80; | |
root /path/to/app/current/public; | |
access_log /var/log/nginx/appname.log; | |
error_log /var/log/nginx/appname.log; | |
rewrite_log on; | |
server_name domainname.com; | |
# 200mb upload max | |
client_max_body_size 200M; | |
# Rewrite non www | |
if ($host ~* www\.(.*)) { | |
set $host_without_www $1; | |
rewrite ^(.*)$ http://$host_without_www$1 permanent; | |
} | |
# Show a maintenance page if one exists. | |
if (-f $document_root/system/maintenance.html) { | |
rewrite ^(.*)$ /system/maintenance.html break; | |
} | |
error_page 500 502 503 504 $document_root/500.html; | |
error_page 404 $document_root/404.html; | |
# try_files attempts to use files in public first. it is more efficient than | |
# running if statements each request (and is the preferred method from nginx docs) | |
# sets up unicorn as the fallback url so anything not found ends up there | |
try_files $uri/index.html $uri.html $uri @unicorn; | |
location @unicorn { | |
proxy_redirect off; | |
proxy_set_header Host $host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_connect_timeout 90; | |
proxy_send_timeout 90; | |
proxy_read_timeout 90; | |
proxy_buffer_size 4k; | |
proxy_buffers 4 32k; | |
proxy_busy_buffers_size 64k; | |
proxy_temp_file_write_size 64k; | |
proxy_pass http://unicorn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment