Skip to content

Commit

Permalink
adds Unicorn and nginx configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Dec 3, 2011
1 parent 9afa7e6 commit 5c13a07
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
57 changes: 57 additions & 0 deletions config/nginx.conf
@@ -0,0 +1,57 @@
upstream rails-contributors {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/tmp/rails-contributors.sock fail_timeout=0;
}

server {
listen 80 deferred;

client_max_body_size 4G;
server_name contributors.rubyonrails.org;

# ~2 seconds is often enough for most folks to parse HTML/CSS and
# retrieve needed images/icons/frames, connections are cheap in
# nginx so increasing this is generally safe...
keepalive_timeout 5;

# Path for static files
root /home/rails/rails-contributors/public;

if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}

# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
try_files $uri/index.html $uri.html $uri @app;

location ~ ^/(images|javascripts|stylesheets)/ {
expires max;
break;
}

location @app {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;

# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;

proxy_pass http://rails-contributors;
}

# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/rails/rails-contributors/public;
}
}
11 changes: 11 additions & 0 deletions config/unicorn.rb
@@ -0,0 +1,11 @@
require 'pathname'

app_root = Pathname.new(__FILE__).dirname.parent.expand_path.to_s

worker_processes 4
working_directory app_root
listen "/tmp/rails-contributors.sock"
timeout 30
pid "#{app_root}/tmp/pids/unicorn.pid"
stderr_path "#{app_root}/log/unicorn.stderr.log"
stdout_path "#{app_root}/log/unicorn.stdout.log"
68 changes: 68 additions & 0 deletions config/unicorn.sh
@@ -0,0 +1,68 @@
#!/bin/sh

set -e
TIMEOUT=${TIMEOUT-60}
APP_ROOT=$HOME/rails-contributors
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="$HOME/.rvm/bin/ruby $APP_ROOT/bin/unicorn_rails -D -c $APP_ROOT/config/unicorn.rb -E production"
action="$1"
set -u

old_pid="$PID.oldbin"

cd $APP_ROOT || exit 1

sig () {
test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}

case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
$CMD
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo

if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
$CMD
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac

0 comments on commit 5c13a07

Please sign in to comment.