【nginx平滑升级演示】
目录
1 、Nginx平滑升级原理
2 、Nginx信号
2.1、主进程支持的信号
2.2、 工作进程支持的信号
3、 平滑升级
3.1 、Nginx添加新模块
1 、Nginx平滑升级原理
-
在不停掉老进程的情况下,启动新进程。
-
老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
-
新进程接受新请求。
-
老进程处理完所有请求,关闭所有连接后,停止。
2 、Nginx信号
2.1、主进程支持的信号
TERM,INT:立刻退出;
QUIT:等待工作进程结束后再退出;
KILL:强制终止进程;
HUP:重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程;
USR1:重新打开日志文件;
USR2:启动新的主进程,实现热升级;
WINCH:逐步关闭工作进程。
2.2、 工作进程支持的信号
TERM,INT:立刻退出;
QUIT:等待请求处理结束后再退出;
USR1:重新打开日志文。
3、 平滑升级
案例:
3.1 、Nginx添加新模块
在已编译安装Nginx的基础上添加/home/nginx_upstream_check_module-master模块。
(1)进入Nginx解压目录
[root@localhost ~]# cd nginx-1.25.3/
(2)查看Nginx已安装的模块
[root@localhost nginx-1.25.3]# nginx -V
nginx version: nginx/1.25.3
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx25 --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-openssl-opt=yes --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug
(3)添加新模块
[root@localhost nginx-1.27.3]# ./configure --prefix=/usr/local/nginx25 --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-openssl-opt=yes --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --add-module=/home/nginx_upstream_check_module-master && make
(4)进行make操作
按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖。
(5)备份原Nginx二进制文件
[root@localhost nginx-1.27.3]# mv /usr/local/nginx25/sbin/nginx /usr/local/nginx25/sbin/nginx.bak
(6)复制新的nginx二进制文件,进入新的nginx源码包
[root@localhost nginx-1.27.3]# cp objs/nginx /usr/local/nginx25/sbin
(7)测试新版本的nginx是否正常
[root@localhost nginx-1.27.3]# nginx -t
nginx: the configuration file /usr/local/nginx25/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx25/conf/nginx.conf test is successful
(8)给nginx发送平滑迁移信号
若不清楚Nginx.pid的路径,可查看nginx配置文件,里面有对应的路径。
[root@localhost nginx-1.27.3]# kill -USR2 `cat /usr/local/nginx25/logs/nginx.pid `
[root@localhost nginx-1.27.3]# ps -aux | grep nginx
root 88555 0.0 0.1 10112 2572 ? Ss 20:52 0:00 nginx: master process /usr/local/nginx25/sbin/nginx
nginx 88556 0.0 0.2 13864 5260 ? S 20:52 0:00 nginx: worker process
root 88948 0.0 0.3 10112 6656 ? S 20:54 0:00 nginx: master process /usr/local/nginx25/sbin/nginx
nginx 88949 0.0 0.2 13864 5260 ? S 20:54 0:00 nginx: worker process
root 89341 0.0 0.1 221680 2432 pts/1 S+ 20:56 0:00 grep --color=auto nginx
(9)查看nginx pid,会出现一个nginx.pid.oldbin
即在不停掉老进程的情况下启动新进程。
[root@localhost nginx-1.27.3]# ll /usr/local/nginx25/logs/
总用量 12
-rw-r--r--. 1 root root 0 9月 29 20:00 access.log
-rw-r--r--. 1 root root 879 9月 29 20:55 error.log
-rw-r--r--. 1 root root 6 9月 29 20:54 nginx.pid
-rw-r--r--. 1 root root 6 9月 29 20:52 nginx.pid.oldbin
(10)关闭旧的Nginx进程
[root@localhost nginx-1.27.3]# kill -WINCH `cat /usr/local/nginx25/logs/nginx.pid.oldbin`
[root@localhost nginx-1.27.3]# !ps
ps -aux | grep nginx
root 88555 0.0 0.1 10112 2572 ? Ss 20:52 0:00 nginx: master process /usr/local/nginx25/sbin/nginx
root 88948 0.0 0.3 10112 6656 ? S 20:54 0:00 nginx: master process /usr/local/nginx25/sbin/nginx
nginx 88949 0.0 0.2 13864 5260 ? S 20:54 0:00 nginx: worker process
root 90196 0.0 0.1 221680 2432 pts/1 S+ 20:59 0:00 grep --color=auto nginx
(11)结束工作进程,完成此次升级
[root@localhost nginx-1.27.3]# kill -QUIT `cat /usr/local/nginx25/logs/nginx.pid.oldbin`
[root@localhost nginx-1.27.3]# !ps
ps -aux | grep nginx
root 88948 0.0 0.3 10112 6656 ? S 20:54 0:00 nginx: master process /usr/local/nginx25/sbin/nginx
nginx 88949 0.0 0.2 13864 5260 ? S 20:54 0:00 nginx: worker process
root 90592 0.0 0.1 221680 2432 pts/1 S+ 21:01 0:00 grep --color=auto nginx
(12)验证Nginx是否升级成功
[root@localhost nginx-1.27.3]# nginx -v
nginx version: nginx/1.27.3