Skip to content

Instantly share code, notes, and snippets.

@andreadipersio
Last active January 20, 2022 14:14
Show Gist options
  • Save andreadipersio/7804185 to your computer and use it in GitHub Desktop.
Save andreadipersio/7804185 to your computer and use it in GitHub Desktop.
How to switch nginx binary at runtime.
# inspired by
# Master NGINX - Dimitri Aivaliotis
# http://www.amazon.com/Mastering-NGINX-Dimitri-Aivaliotis-ebook/dp/B00B90PJR4
# Chapter 8 - Switching binaries at runtime
# We get the pid of the 'to be replaced' nginx master process
export nginx_old_pid=`cat /var/run/nginx.pid`
# we send an USR2 signal to tell 'to be replaced' process
# to start a new master process.
# Notice that NGINX will rename the pid file, example:
# /var/run/nginx.pid.oldbin
kill -USR2 $nginx_old_pid
# Sending the 'to be replaced' process a WINCH
# cause it to stop handling new requests and phase out
# its worker processed once they are done with their current requests.
kill -WINCH $nginx_old_pid
# Once all workers process of the 'to be replaced' nginx master process
# have ended, we can send it a QUIT signal.
kill -QUIT $nginx_old_pid
# ...or, if we discover problem with the new binary,
# we can rollback to the old one by sending a HUP signal
kill -HUP $nginx_old_pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment