Skip to content

Instantly share code, notes, and snippets.

@sax
Created September 7, 2010 22:49
Show Gist options
  • Save sax/569288 to your computer and use it in GitHub Desktop.
Save sax/569288 to your computer and use it in GitHub Desktop.
unicorn + nginx on launchd
user sax staff;
worker_processes 1;
daemon off;
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# Set this to on if you have more than 1 working processes
# This will allow only one child to watch the pollset and accept
# a connection to a socket
accept_mutex off; # "on" if nginx worker_processes > 1}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
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 {
# This is the socket we configured in unicorn.rb
server unix:/var/tmp/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name project.local;
root /Users/sax/Projects/nginx_assets;
# passenger_enabled on;
# rails_env stage;
# serve static files
set $asset F;
if ($request_filename ~* ".*\.(gif|jpg|css|jpeg|png|bmp|asp|php|_vti_bin|js)$") {
set $asset T;
}
if (!-f $request_filename) {
set $asset "${asset}T";
}
if ($asset = TT)
{
return 404;
break;
}
# unicorn
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn_server;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /Users/sax/Projects/nginx_assets;
}
}
}
#!/bin/sh
NGINX=/usr/local/sbin/nginx
LAUNCHCTL=/System/Library/LaunchDaemons/nginx.plist
find_nginx_pid(){
echo `ps -ef | grep nginx | grep master | awk '{printf $2;}'`;
}
stop_nginx(){
echo "Killing Nginx process"
kill -6 $(find_nginx_pid)
}
restart_nginx(){
echo "Restarting Nginx process"
kill -HUP $(find_nginx_pid)
}
launch_nginx(){
echo "Launching Nginx daemon"
launchctl load ${LAUNCHCTL}
}
start_nginx(){
echo "Starting Nginx"
`${NGINX}`
}
check_nginx_running(){
if [ "$(find_nginx_pid)" ]; then
return 0
else
echo "Nginx is not currently running"
return 1
fi
}
warn_already_running(){
if [ "$(find_nginx_pid)" ]; then
echo "Nginx is already running"
return 0
else
return 1
fi
}
show_pid(){
echo $(find_nginx_pid)
}
case $1 in
start)
warn_already_running || start_nginx
;;
stop)
check_nginx_running && stop_nginx
;;
restart)
check_nginx_running && restart_nginx
;;
launch)
warn_already_running || launch_nginx
;;
pid)
check_nginx_running && show_pid
;;
*)
echo "usage : [sudo] nginxctl [pid|start|stop|restart|launch]"
exit 1
;;
esac
exit $?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>unicorn</string>
<key>EnvironmentVariables</key>
<dict>
<key>GEM_HOME</key><string>/Users/sax/.rvm/gems/ruby-1.8.7-p299@mc</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/Users/sax/.rvm/gems/ruby-1.8.7-p299@mc/bin/unicorn_rails</string>
<string>-c</string>
<string>/Users/sax/Projects/unicorn.rb</string>
<string>-E</string>
<string>stage</string>
<string>-D</string>
</array>
<key>KeepAlive</key><true/>
<key>StandardErrorPath</key><string>/var/log/unicorn/unicorn.stderr.log</string>
<key>LaunchOnlyOnce</key><true/>
</dict>
</plist>
worker_processes 2
working_directory "/Users/sax/Projects/mc/"
# This loads the application in the master process before forking
# worker processes
# Read more about it here:
# http://unicorn.bogomips.org/Unicorn/Configurator.html
preload_app true
timeout 30
user 'sax', 'staff'
# This is where we specify the socket.
# We will point the upstream Nginx module to this socket later on
listen "/var/tmp/unicorn.sock", :backlog => 64
pid "/var/tmp/unicorn.pid"
# Set the path of the log files inside the log folder of the testapp
stderr_path "/var/log/unicorn/unicorn.stderr.log"
stdout_path "/var/log/unicorn/unicorn.stdout.log"
before_fork do |server, worker|
# This option works in together with preload_app true setting
# What is does is prevent the master process from holding
# the database connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
# Here we are establishing the connection after forking worker
# processes
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
#!/bin/sh
UNICORN="unicorn_rails"
ASSETS_LINK=$HOME/Projects/nginx_assets
LAUNCHCTL="unicorn.plist"
PROJECT=${2:-"project"}
RAILS_ENV=${3:-"development"}
find_unicorn_pid(){
ps -ef | grep unicorn | grep master | awk '{printf $2;}'
}
find_unicorn_settings_file(){
find ~ -name unicorn\:$(echo $PROJECT | sed 's/\//\:/g').rb
}
find_project_directory(){
find ~ -regex ".*/$PROJECT"
}
find_unicorn_plist(){
echo `find ~ -name $LAUNCHCTL`;
}
link_assets_dir(){
echo "Linking assets directory"
unlink_assets_dir
ln -s $1/public $ASSETS_LINK
}
unlink_assets_dir(){
rm $ASSETS_LINK 2>/dev/null
}
stop_unicorn(){
echo "Killing unicorn process"
kill -QUIT $(find_unicorn_pid)
echo "Cleaning up asset dir"
unlink_assets_dir
}
restart_unicorn(){
echo "Restarting unicorn process"
kill -HUP $(find_unicorn_pid)
}
launch_unicorn(){
echo "Launching unicorn daemon"
launchctl load $(unicorn_plist) && \
link_assets_dir $(find_project_directory)
}
start_unicorn(){
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
conf=$(find_unicorn_settings_file)
dir=$(find_project_directory)
echo "Starting unicorn in $RAILS_ENV mode"
echo "Project : $dir"
echo "Configuration : $conf"
cd $dir && \
bundle exec $UNICORN -c $conf -E $RAILS_ENV -D && \
link_assets_dir $dir
}
check_unicorn_running(){
if [ "$(find_unicorn_pid)" ]; then
return 0
else
echo "unicorn is not currently running"
return 1
fi
}
warn_already_running(){
if [ "$(find_unicorn_pid)" ]; then
echo "unicorn is already running"
return 0
else
return 1
fi
}
show_pid(){
echo $(find_unicorn_pid)
}
case $1 in
start)
warn_already_running || start_unicorn
;;
stop)
check_unicorn_running && stop_unicorn
;;
restart)
check_unicorn_running && restart_unicorn
;;
launch)
warn_already_running || launch_unicorn
;;
pid)
check_unicorn_running && show_pid
;;
*)
echo "usage : [sudo] unicornctl [pid|start|stop|restart|launch]"
exit 1
;;
esac
exit $?
@mikhailov
Copy link

thanks, it's pretty useful configs! see our configurations https://gist.github.com/3052776

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