Nginx smooth upgrade detailed operation method
- 2020-05-07 20:53:13
- OfStack
1. Smooth upgrade overview
Nginx conveniently helps us with smooth upgrades. The principle is as follows:
(1) start a new process while dropping the old one.
(2) the old process is responsible for processing requests that are still pending, but it no longer accepts processing requests.
(3) the new process accepts the new request.
(4) after the old process has processed all requests and closed all connections, it stops.
This is very convenient to achieve a smooth upgrade. There are generally two situations where you need to upgrade Nginx, one is when you do upgrade Nginx, and the other is when you add a new module to Nginx.
2. The upgrade process
The specific operation is also very simple, as follows:
(0) view the current version
In the directory where the Nginx executable is stored, type:
./nginx -V
(1) download the new Nginx version and compile it.
wget nginx-1.0.11.tar.gz
tar zxvf nginx-1.0.11.tar.gz
cd nginx-1.0.11
./configure --add-module=/customized_module_0 --add-module=/customized_module_1
make
Be careful not to proceed with make install
(2) back up the old version of the executable
cd /usr/local/nginx/sbin
sudo cp nginx nginx.old
(3) modify the configuration file
Make configuration file changes if necessary.
(4) copy the new executable
sudo cp /home/michael/tmp/nginx-1.0.11/objs/nginx /usr/local/nginx/sbin/
(5) upgrade
cd /home/michael/tmp/nginx-1.0.11
make upgrade
(6) clean up redundant files
rm -rf /home/michael/tmp/nginx-1.0.11
(7) view the Nginx version
cd /usr/local/nginx/sbin
./nginx -V
3. Observe the process change
It can be observed on my machine that I configured 10 worker processes, and observed after starting:
root 6241 10419 0 10:51 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 6242 6241 2 10:51 ? 00:00:00 nginx: worker process
nobody 6243 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6244 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6245 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6246 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6247 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6248 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6249 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6250 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6251 6241 1 10:51 ? 00:00:00 nginx: worker process
nobody 6252 6241 0 10:51 ? 00:00:00 nginx: cache manager process
nobody 6253 6241 0 10:51 ? 00:00:00 nginx: cache loader process
luming 6310 25051 0 10:51 pts/1 00:00:00 grep --color=auto nginx
nobody 7995 10419 0 Jan12 ? 00:20:37 nginx: worker process is shutting down
nobody 7996 10419 0 Jan12 ? 00:20:11 nginx: worker process is shutting down
nobody 7998 10419 0 Jan12 ? 00:20:04 nginx: worker process is shutting down
nobody 8003 10419 0 Jan12 ? 00:20:12 nginx: worker process is shutting down
root 10419 1 0 Jan08 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
You can see that the new process has 1 master and 10 worker, plus 1 old master (which can be seen from the time) and 4 worker (the other 6 old worker have already processed all the connections and shutdown has). There is also an loader process. In a few seconds you'll see that worker has only two:
root 6241 10419 0 10:51 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 6242 6241 1 10:51 ? 00:00:00 nginx: worker process
nobody 6243 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6244 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6245 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6246 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6247 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6248 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6249 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6250 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6251 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6252 6241 0 10:51 ? 00:00:00 nginx: cache manager process
nobody 6253 6241 0 10:51 ? 00:00:00 nginx: cache loader process
luming 6430 25051 0 10:51 pts/1 00:00:00 grep --color=auto nginx
nobody 7996 10419 0 Jan12 ? 00:20:11 nginx: worker process is shutting down
nobody 8003 10419 0 Jan12 ? 00:20:12 nginx: worker process is shutting down
root 10419 1 0 Jan08 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
After 1 more minute observation:
root 6241 1 0 10:51 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 6242 6241 0 10:51 ? 00:00:01 nginx: worker process
nobody 6243 6241 0 10:51 ? 00:00:01 nginx: worker process
nobody 6244 6241 0 10:51 ? 00:00:01 nginx: worker process
nobody 6245 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6246 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6247 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6248 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6249 6241 0 10:51 ? 00:00:00 nginx: worker process
nobody 6250 6241 0 10:51 ? 00:00:01 nginx: worker process
nobody 6251 6241 0 10:51 ? 00:00:02 nginx: worker process
nobody 6252 6241 0 10:51 ? 00:00:00 nginx: cache manager process
luming 8680 25051 0 10:56 pts/1 00:00:00 grep --color=auto nginx
Congratulations! You can upgrade your Nginx server gracefully.