Tuesday, July 24, 2007

Web Server Restart (nginx) Without Downtime

One of the reasons we've migrated our hosted websites to nginx is not only performance but some notable features that were not, at the time, available for Apache. They may not be available but the longer I work with nginx the less inclined I am to switch back.

If you would like to restart nginx without losing incoming connections during the 'restart process' just do the following, taken directly from the English wiki.

user@machine ~> ps aux | grep -i nginx
root 2213 0.0 0.0 6784 2036 ? Ss 03:01 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
user@machine ~> kill -HUP 2213

What happens is that when nginx receives the HUP signal, it tries to parse the configuration file (the specified one, if present, otherwise the default), and if successful, tries to apply a new configuration (i.e. re-open the log files and listen sockets). If successful, nginx runs new worker processes and signals graceful shutdown to old workers. Notified workers close listen sockets but continue to serve current clients. After serving all clients old workers shutdown. If nginx wasn't successful in applying the new configuration, it continues to work with an old configuration.

Note: I've edited the ps command to be more Linux cross-distro friendly so you might see some child processes as well but you want to issue the kill command against the master process id.

Even better:
user@machine ~> kill -HUP `cat /var/run/nginx.pid`

No comments: