Wednesday, October 15, 2014

Quick Tip: nginx.conf file for upstart in Ubuntu 14.04

It tookme several tries to find the right configuration file for nginx to work with upstart (for example Ubunty 14.04 uses upstart)

The configuration file is based in the oficial nginx file, but modified to allow restarts (*)


# nginx
 
description "nginx http daemon"
author "George Shammas "
 
start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]
 
env DAEMON=/usr/sbin/nginx
env PID=/var/run/nginx.pid
 
expect fork
respawn
respawn limit 10 5
 
pre-start script
        $DAEMON -t
        if [ $? -ne 0 ]
                then exit $?
        fi
end script
 
# Ensure nginx is shutdown gracefully
# Upstart will be tracking the wrong PID so the following is needed to stop nginx
post-stop exec start-stop-daemon --stop --pidfile $PID --name nginx --exec $DAEMON --signal QUIT

exec $DAEMON -c /etc/nginx/conf/nginx.conf
--- (*) Without the post-stop script, nginx didnt kill the forked processed and spit a 502 Bad Gateway . #you can see taht children are not killed after a `service nginx stop` sudo netstat -tulpn #to kill stalled children sudo fuser -k 80/tcp