Skip to content

Instantly share code, notes, and snippets.

@basiszwo
Created August 27, 2013 13:10
Show Gist options
  • Save basiszwo/6353310 to your computer and use it in GitHub Desktop.
Save basiszwo/6353310 to your computer and use it in GitHub Desktop.
Nginx / unicorn setup / configuration

Installation assuming Ubuntu

apt-get install nginx

Nginx Configuration

  • see nginx config file below
  • see nginx upstart config below

Unicorn config

In case the user home is encrypted it's not possible to have unicorn autostartet!!

# /etc/nginx/nginx.conf
worker_processes 1;
user YYYYY YYYYY;
pid /var/run/nginx.pid;
error_log /var/log/nginx.error.log;
events {
worker_connections 1024;
accept_mutex off;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx.access.log combined;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream unicorn_server {
server unix:/home/YYYYY/www/XXXXX/current/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
server_name _;
if ($host !~* 'www.XXXXX\.com' ) {
rewrite ^/(.*)$ http://www.XXXXX.com/$1 permanent;
}
keepalive_timeout 5;
proxy_read_timeout 600s;
root /home/YYYYY/www/XXXXX/current/public;
location /nginx_status {
#stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location / {
if (-f $document_root/maintenance.html) {
return 503;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://unicorn_server;
break;
}
}
error_page 500 502 504 /500.html;
location = /500.html {
root /home/YYYYY/www/XXXXX/current/public;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
error_page 403 404 /404.html;
location = /404.html {
root /home/YYYYY/www/XXXXX/current/public;
}
#location ~ ^/assets|images|stylesheets|javascripts/ {
# expires max;
# access_log off;
#}
location ~* \.(ico|css|js|gif|jpe?g|png|swf)(\?[0-9]+)?$ {
expires max;
access_log off;
break;
}
}
}
description "Unicorn"
# starting unicorn with bundler, and a user contained rvm:
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
chdir /home/XXXXX/www/YYYYY/current
env RAILS_ENV=production
# as the unicorn master detaches itself (and you probably want to restart it safely with capistrano), a working solution is to use the pre-start and post-stop flags:
pre-start script
exec su - XXXXX -c "cd /home/XXXXX/www/YYYYY/current; source ~/.rvm/scripts/rvm; ~/.rvm/bin/rvm-shell -c 'bundle exec unicorn -c config/unicorn.rb -E production -D'"
end script
post-stop exec kill `cat /home/XXXXX/www/YYYYY/current/tmp/pids/unicorn.pid`
worker_processes 3 # amount of unicorn workers to spin up
timeout 30 # restarts workers that hang for 30 seconds
# properly handle new relic heroku addon
preload_app true
# config/unicorn.rb
# -*- encoding : utf-8 -*-
APP_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__)))
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
begin
gems_path = ENV['MY_RUBY_HOME'].split(/@/)[0].sub(/rubies/,'gems')
ENV['GEM_PATH'] = "#{gems_path}:#{gems_path}@global"
require 'rvm'
RVM.use_from_path! APP_ROOT
rescue LoadError
raise "RVM gem is currently unavailable."
end
end
# If you're not using Bundler at all, remove lines bellow
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
require 'bundler/setup'
worker_processes 4
working_directory APP_ROOT
preload_app true
timeout 600
listen APP_ROOT + "/tmp/sockets/unicorn.sock", :backlog => 64
pid APP_ROOT + "/tmp/pids/unicorn.pid"
stderr_path APP_ROOT + "/log/unicorn.stderr.log"
stdout_path APP_ROOT + "/log/unicorn.stdout.log"
before_fork do |server, worker|
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
old_pid = APP_ROOT + '/tmp/pids/unicorn.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
puts "Old master alerady dead"
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment