服务器平滑升级:
1. 在不停掉老进程的情况下,启动新进程。
2. 老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
3. 新进程接受新请求。
4. 老进程处理完所有请求,关闭所有连接后,停止
Nginx信号简介
主进程支持的信号
TERM, INT: 立刻退出
QUIT: 等待工作进程结束后再退出
KILL: 强制终止进程
HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
USR1: 重新打开日志文件
USR2: 启动新的主进程,实现热升级
WINCH: 逐步关闭工作进程
nginx平滑升级:
1.查看旧版nginx的编译参数
[root@localhost ~]# nginx -V
2、到官网下载新版源码包(nginx官网)
[root@localhost ~]#wget http://nginx.org/download/nginx-1.19.4.tar.gz
3、解压到指定路径
[root@localhost ~]#tar -xf nginx-1.19.4.tar.gz -C /usr/src
4、准备环境
[root@localhost ~]yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel
5、编译新版本Nginx源码包,安装路径需与旧版一致,注意:不要执行make install
[root@localhost ~]#cd /usr/src/nginx-1.19.4/
[root@localhost nginx-1.19.4]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module&&make
6、备份二进制文件,用新版本的替换
```powershell
[root@localhost nginx-1.19.4]#mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@localhost nginx-1.19.4]#cp objs/nginx /usr/local/nginx/sbin/
7、语法检查
[root@localhost nginx-1.19.4]#/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
8、发送USR2信号
向主进程(master)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@localhost nginx-1.19.4]# ps aux | grep nginx
root 16396 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21993 0.0 0.2 112724 996 pts/1 S+ 20:42 0:00 grep --color=auto nginx
[root@localhost nginx-1.19.4]#kill -USR2 16396
[root@localhost nginx-1.19.4]# ps aux | grep nginx
root 16396 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
root 21997 0.0 0.2 112724 996 pts/1 R+ 20:44 0:00 grep --color=auto nginx
9、发送WINCH信号
向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@localhost nginx-1.19.4]#kill -WINCH 16396
[root@localhost nginx-1.19.4]# ps aux | grep nginx
root 16396 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
注意:回滚步骤,发送HUP信号
如果这时需要回退继续使用旧版本,可向旧的Nginx主进程发送HUP信号,它会重新启动工作进程, 仍使用旧版配置文件。然后可以将新版Nginx进程杀死(使用QUIT、TERM、或者KILL)
[root@localhost nginx-1.19.4]#kill -HUP 16396
10、发送QUIT信号
升级完毕,可向旧的Nginx主进程(master)发送(QUIT、TERM、或者KILL)信号,使旧的主进程退出
[root@localhost nginx-1.19.4]#kill -QUIT 16396
[root@localhost nginx-1.19.4]# ps aux | grep nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
11、验证nginx版本号,并访问测试
[root@localhost nginx-1.19.4]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.19.4