Upgrading To A New Nginx Binary In Different Place On The Fly

Recently we need use some features which provided by the new Nginx release. And the current package is provided by the company's system team and they don't provide a new version package. So we need build it by ourselves.

Because the orignal package is installed by yum and we want to deploy new package by different method, so putting the new package in different place maybe better. So we face such a problem:

We want to upgrade Nginx to a new version, and the new version binary and all its dependency is put in different place. In the same time, the server must provide serivce continous without downtime.

How To Upgrade Nginx In-Place Without Dropping Client Connections talks about upgrading nginx in-place.

Nginx release 1.9.1 introduces a new feature that enables use of SO_REUSEPORT. With this we can complete the task.

Operation Steps

First we compile new Nginx release and set absolute path for --prefix etc. Deploy the output on the machine. Enables reuseport directive for new packages.

Copy <NEW_PACKAGE>/sbin/nginx to <OLD_PACKAGE>/sbin/nginx, and upgrade Nginx with the new <OLD_PACKAGE>/sbin/nginx binary using the methods from How To Upgrade Nginx In-Place Without Dropping Client Connections.

Start another Nginx master using <NEW_PACKAGE>/sbin/nginx. Because reuseport enabled, we can start two masters by hand. And then stop master launched by <OLD_PACKAGE>/sbin/nginx gracefully. After 'old' nginx master stopped, <NEW_PACKAGE>/run/nginx.pid maybe deleted, because we have two masters write its process id into same file. We just generate this file for the last master by hand.

Remove reuseport directive in configure file, and then reload Nginx.