Nginx- hot deployment

1. Purpose

Upgrading typically add new modules, or upgrade. We will demonstrate the following add echo-nginx-module-0.61 module.

First download and extract the echo-nginx-module-0.61 nginx module installation package to the same directory:

[root@localhost nginx-1.16.1]# ls /usr/src
debug  echo-nginx-module-0.61  echo-nginx-module-0.61.tar.gz  kernels  nginx-1.16.1  nginx-1.16.1.tar.gz

Option One:

Add modules, recompile

[root@localhost nginx-1.16.1]# pwd
/usr/src/nginx-1.16.1
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --add-module=../echo-nginx-module-0.61
[root@localhost nginx-1.16.1]#make
[root@localhost nginx-1.16.1]#make install

#只会重新生成二进制文件,不会覆盖其他文件
[root@localhost nginx-1.16.1]# ls /usr/local/nginx/sbin/
nginx  nginx.old

View pid

[root@localhost nginx-1.16.1]# ps -ef|grep nginx
root     13529     1  0 19:10 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13530 13529  0 19:10 ?        00:00:00 nginx: worker process
nobody   13531 13529  0 19:10 ?        00:00:00 nginx: worker process
nobody   13532 13529  0 19:10 ?        00:00:00 nginx: worker process
nobody   13533 13529  0 19:10 ?        00:00:00 nginx: worker process
root     13595  9891  0 19:17 pts/0    00:00:00 grep --color=auto nginx

upgrade

[root@localhost nginx-1.16.1]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

Check again pid, and upgrade different pre-pid, the upgrade is complete

[root@localhost nginx-1.16.1]# ps -ef|grep nginx
root     13601     1  0 19:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13603 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13604 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13605 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13606 13601  0 19:17 ?        00:00:00 nginx: worker process
root     13612  9891  0 19:17 pts/0    00:00:00 grep --color=auto nginx

Option II:

The program to restore a binary file, a program two tests

[root@localhost nginx-1.16.1]# rm -rf /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.16.1]# mv /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V
nginx version: MYWS/1.8
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 

View pid

[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root     13601     1  0 19:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13603 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13604 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13605 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13606 13601  0 19:17 ?        00:00:00 nginx: worker process
root     13646  9891  0 19:28 pts/0    00:00:00 grep --color=auto nginx

Generate a new master process

[root@localhost nginx-1.16.1]# kill -USR2 13601

View pid again, old and new processes exist. Once the new process is pulled up, all requests are processed by the new process, the process will no longer deal with the old

[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root     13601     1  0 19:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13603 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13604 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13605 13601  0 19:17 ?        00:00:00 nginx: worker process
nobody   13606 13601  0 19:17 ?        00:00:00 nginx: worker process
root     13647 13601  0 19:29 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13648 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13649 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13650 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13651 13647  0 19:29 ?        00:00:00 nginx: worker process
root     13653  9891  0 19:29 pts/0    00:00:00 grep --color=auto nginx

Elegant old worker process exit

[root@localhost nginx-1.16.1]# kill -WINCH 13601
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root     13601     1  0 19:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root     13647 13601  0 19:29 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13648 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13649 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13650 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13651 13647  0 19:29 ?        00:00:00 nginx: worker process
root     13656  9891  0 19:33 pts/0    00:00:00 grep --color=auto nginx

Choice 1: a period of observation If the new process is no problem, get rid of the old process, the upgrade is complete

kill -QUIT 13601

Choice 2: If the new period of observation process has a problem, then re-pull the old process

[root@localhost nginx-1.16.1]# kill -HUP 13601
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root     13601     1  0 19:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root     13647 13601  0 19:29 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13648 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13649 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13650 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13651 13647  0 19:29 ?        00:00:00 nginx: worker process
nobody   13658 13601  0 19:35 ?        00:00:00 nginx: worker process
nobody   13659 13601  0 19:35 ?        00:00:00 nginx: worker process
nobody   13660 13601  0 19:35 ?        00:00:00 nginx: worker process
nobody   13661 13601  0 19:35 ?        00:00:00 nginx: worker process
root     13663  9891  0 19:35 pts/0    00:00:00 grep --color=auto nginx

Get rid of the new process, to restore the old process

[root@localhost nginx-1.16.1]# kill -QUIT 13647
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root     13601     1  0 19:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   13658 13601  0 19:35 ?        00:00:00 nginx: worker process
nobody   13659 13601  0 19:35 ?        00:00:00 nginx: worker process
nobody   13660 13601  0 19:35 ?        00:00:00 nginx: worker process
nobody   13661 13601  0 19:35 ?        00:00:00 nginx: worker process
root     13665  9891  0 19:36 pts/0    00:00:00 grep --color=auto nginx
Published 34 original articles · won praise 1 · views 520

Guess you like

Origin blog.csdn.net/weixin_42440154/article/details/103515397