平滑升级与回退Nginx版本
平滑升级与回退Nginx版本

平滑升级与回退Nginx版本

Nginx 的平滑升级是指在不中断服务的情况下进行软件版本或配置文件的更新。通过平滑升级,Nginx能够在运行时应用新的配置或软件版本,继续处理请求,而不影响现有的连接。

查看当前Nginx版本

root@wh:~# apt update
root@wh:~# nginx -v
nginx version: nginx/1.24.0 (Ubuntu)

创建一个目录 用于放新版本文件

一次升级尽量不要跨多个大版本升级

root@wh:~# mkdir /data/softs -p ; cd /data/softs
root@wh:/data/softs# wget http://nginx.org/download/nginx-1.26.0.tar.gz
--2025-04-30 15:17:08--  http://nginx.org/download/nginx-1.26.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2601::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 1244118 (1.2M) [application/octet-stream]
正在保存至: ‘nginx-1.26.0.tar.gz’

nginx-1.26.0.tar.gz               100%[===========================================================>]   1.19M  1.13MB/s    用时 1.0s

2025-04-30 15:17:09 (1.13 MB/s) - 已保存 ‘nginx-1.26.0.tar.gz’ [1244118/1244118])
root@wh:/data/softs# ls
nginx-1.26.0.tar.gz

per环境准备 – 基于apt方式安装的nginx的场景中才会出现

如果 make 编译的时候,发生
make[1]: *** [objs/Makefile:1584:objs/ngx_http_perl_module.so] 错误 1
make[1]: 离开目录“/data/softs/nginx-1.26.0”
make: *** [Makefile:10:build] 错误 2


查看perl环境
root@ubuntu24:nginx-1.26.2# find / -name libperl.so*
/usr/lib/x86_64-linux-gnu/libperl.so.5.38
/usr/lib/x86_64-linux-gnu/libperl.so.5.38.2

结果显示,库文件都是携带版本号的,编译的时候,很多是不带版本号的,所以创建一个软连接
ln -s /usr/lib/x86_64-linux-gnu/libperl.so.5.38 /usr/lib/x86_64-linux-gnu/libperl.so

安装需要的编译工具

基于旧的nginx配置,来编译安装新版本软件

root@wh:~# apt install build-essential gcc g++ libc6 libc6-dev libpcre3 libpcre3-dev libssl-dev libsystemd-dev zlib1g-dev  -y

root@wh:~# apt install libxml2 libxml2-dev libxslt1-dev php-gd libgd-dev geoip-database libgeoip-dev  -y

root@wh:/data/softs# cd nginx-1.26.0/

#需要更改一下编译版本号,建议放入文件中,使用" %s/ 原版本号/目标版本号 /g "进行更改

root@wh:/data/softs/nginx-1.26.0# ./configure --with-cc-opt='-g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/nginx-DlMnQR/nginx-1.26.0=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/build/nginx-DlMnQR/nginx-1.26.0=/usr/src/nginx-1.26.0-2ubuntu7.1 -fPIC -Wdate-time -D_FORTIFY_SOURCE=3' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-http_geoip_module=dynamic --with-http_image_filter_module=dynamic --with-http_perl_module=dynamic --with-http_xslt_module=dynamic --with-mail=dynamic --with-stream=dynamic --with-stream_geoip_module=dynamic

进行配置编译,编译时间大概为两分钟左右

查看编译生成文件

root@wh:/data/softs/nginx-1.26.0# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  new_version.txt  objs  README  src
root@wh:/data/softs/nginx-1.26.0# ls objs/
autoconf.err                            ngx_http_image_filter_module_modules.o  ngx_mail_module.so
Makefile                                ngx_http_image_filter_module.so         ngx_modules.c
nginx                                   ngx_http_perl_module_modules.c          ngx_modules.o
nginx.8                                 ngx_http_perl_module_modules.o          ngx_stream_geoip_module_modules.c
ngx_auto_config.h                       ngx_http_perl_module.so                 ngx_stream_geoip_module_modules.o
ngx_auto_headers.h                      ngx_http_xslt_filter_module_modules.c   ngx_stream_geoip_module.so
ngx_http_geoip_module_modules.c         ngx_http_xslt_filter_module_modules.o   ngx_stream_module_modules.c
ngx_http_geoip_module_modules.o         ngx_http_xslt_filter_module.so          ngx_stream_module_modules.o
ngx_http_geoip_module.so                ngx_mail_module_modules.c               ngx_stream_module.so
ngx_http_image_filter_module_modules.c  ngx_mail_module_modules.o               src

root@wh:/data/softs/nginx-1.26.0# nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
root@wh:/data/softs/nginx-1.26.0# ./objs/nginx -v
nginx version: nginx/1.26.0

root@wh:/data/softs/nginx-1.26.0# ls ./objs/nginx /usr/sbin/nginx  -l
-rwxr-xr-x 1 root root 6030320  4月 30 15:43 ./objs/nginx
-rwxr-xr-x 1 root root 1313752  4月  1 02:38 /usr/sbin/nginx

将1.24.0 版本执行文件进行备份后可将新版本拷贝到执行文件路径

root@wh:/data/softs/nginx-1.26.0# mv /usr/sbin/nginx /usr/sbin/nginx-1.24.0
root@wh:/data/softs/nginx-1.26.0# cp ./objs/nginx /usr/sbin/nginx
root@wh:/data/softs/nginx-1.26.0# ls /usr/sbin/ |grep nginx
nginx
nginx-1.24.0
root@wh:/data/softs/nginx-1.26.0# nginx -v
nginx version: nginx/1.26.0    
#现在执行文件已经为新版本,但web请求头依然是老版本的
root@wh:/data/softs/nginx-1.26.0# curl 127.1 -I
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)   #老版本请求头
Date: Wed, 30 Apr 2025 07:54:46 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 30 Apr 2025 07:11:14 GMT
Connection: keep-alive
ETag: "6811cd12-267"
Accept-Ranges: bytes

向旧版本master进程发起信号,-USR2生成新版本nginx的master进程

root@wh:/data/softs/nginx-1.26.0# ps -aux |grep nginx
root                 5382  0.0  0.0  11156  1588 ?        Ss   15:11   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data    5383  0.0  0.1  12936  4788 ?        S    15:11   0:00 nginx: worker process
www-data    5384  0.0  0.1  12936  4276 ?        S    15:11   0:00 nginx: worker process

root@wh:/data/softs/nginx-1.26.0# kill -USR2 5382
root@wh:/data/softs/nginx-1.26.0# ps -aux |grep nginx
root                 5382  0.0  0.0  11156  2228 ?        Ss   15:11   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data    5383  0.0  0.1  12936  4788 ?        S    15:11   0:00 nginx: worker process
www-data    5384  0.0  0.1  12936  4276 ?        S    15:11   0:00 nginx: worker process
-------以上为旧版本的master进程与worker进程---------以下为新版本的master进程与worker进程----------
root                56900  0.0  0.1  11012  7040 ?        S    15:58   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   56901  0.0  0.1  12760  4408 ?        S    15:58   0:00 nginx: worker process
www-data   56902  0.0  0.1  12760  4408 ?        S    15:58   0:00 nginx: worker process

对服务端进行信号传递,关闭旧worker进程

root@wh:~# kill -WINCH 5382
root@wh:~# ps -aux |grep nginx
root                5382  0.0  0.0  11156  2228 ?        Ss   15:11   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
root                56900  0.0  0.1  11012  7040 ?        S    15:58   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   56901  0.0  0.1  12760  4408 ?        S    15:58   0:00 nginx: worker process
www-data   56902  0.0  0.1  12760  4408 ?        S    15:58   0:00 nginx: worker process
旧版本的worker进程已经被丢弃
root@wh:~# curl 127.0 -I
HTTP/1.1 200 OK
Server: nginx/1.26.0
Date: Wed, 30 Apr 2025 08:02:51 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 30 Apr 2025 07:11:14 GMT
Connection: keep-alive
ETag: "6811cd12-267"
Accept-Ranges: bytes

到此基本结束平滑升级,需要一段时间进程测试,若有问题则回退到老版本,若没问题可以退出老版本的master进程即结束完整升级

root@wh:~# kill -QUIT 5382   #(5382为老版本master进程IP)

若有问题 需要回退到旧版本

备份新版本执行文件,将老版本执行文件备份改为执行文件
root@wh:~# ls /usr/sbin/ |grep nginx
nginx
nginx-1.24.0
root@wh:~# mv /usr/sbin/nginx /usr/sbin/nginx-1.26.0
root@wh:~# mv /usr/sbin/nginx-1.24.0 /usr/sbin/nginx
root@wh:~# nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
root@wh:~# curl 127.1 -I
HTTP/1.1 200 OK
Server: nginx/1.26.0
Date: Wed, 30 Apr 2025 08:11:39 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 30 Apr 2025 07:11:14 GMT
Connection: keep-alive
ETag: "6811cd12-267"
Accept-Ranges: bytes

root@wh:~# ps -aux |grep nginx
root                5382  0.0  0.0  11156  2228 ?        Ss   15:11   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
root                56900  0.0  0.1  11012  7040 ?        S    15:58   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   56901  0.0  0.1  12760  4792 ?        S    15:58   0:00 nginx: worker process
www-data   56902  0.0  0.1  12760  4920 ?        S    15:58   0:00 nginx: worker process
将老版本master进程拉起
root@wh:~# kill -HUP 5382
root@wh:~# pstree -p|grep nginx
           |-nginx(5382)-+-nginx(56900)-+-nginx(56901)
           |             |                                               `-nginx(56902)
           |             |-nginx(57011)    老版本的master进程重新开启两个worker进程
           |             `-nginx(57012)

将新版本master进程退出即可将

root@wh:~# curl 127.1 -I -s |grep Server
Server: nginx/1.26.0   #还是新版本
root@wh:~# kill -QUIT 56900
root@wh:~# curl 127.1 -I -s |grep Server
Server: nginx/1.24.0 (Ubuntu)

若是已经将老版本master进程退出,可以现将执行文件转移备份后启动Nginx即可

root@wh:~# ps -aux |grep nginx
root                5382  0.0  0.0  11156  2228 ?        Ss   15:11   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
root                56900  0.0  0.1  11012  7040 ?        S    15:58   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   56901  0.0  0.1  12760  4792 ?        S    15:58   0:00 nginx: worker process
www-data   56902  0.0  0.1  12760  4408 ?        S    15:58   0:00 nginx: worker process

root@wh:~# kill -QUIT 5382
root@wh:~# curl 127.1 -I -s |grep Server
Server: nginx/1.26.0
root@wh:~# ps -aux |grep nginx
root                56900  0.0  0.1  11012  7040 ?        S    16:12   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   56901  0.0  0.1  12760  4792 ?        S    16:12   0:00 nginx: worker process
www-data   56902  0.0  0.1  12760  4920 ?        S    16:12   0:00 nginx: worker process

root@wh:~# ls /usr/sbin/ |grep nginx
nginx
nginx-1.24.0
root@wh:~# mv /usr/sbin/nginx /usr/sbin/nginx-1.26.0
root@wh:~# mv /usr/sbin/nginx-1.24.0 /usr/sbin/nginx
root@wh:~# nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
root@wh:~# curl 127.1 -I -s |grep Server
Server: nginx/1.26.0


将新版本退出
root@wh:~# ps -aux |grep nginx
root                56900  0.0  0.1  11012  7040 ?        S    16:12   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   56901  0.0  0.1  12760  4792 ?        S    16:12   0:00 nginx: worker process
www-data   56902  0.0  0.1  12760  4920 ?        S    16:12   0:00 nginx: worker process

root@wh:~# kill -QUIT 56900
root@wh:~# ps -aux |grep nginx
root       56981  0.0  0.0   9304  2432 pts/0    S+   16:26   0:00 grep --color=auto nginx
root@wh:~# /usr/sbin/nginx
root@wh:~# ps -aux |grep nginx
root       56984  0.0  0.0  11156  1588 ?        Ss   16:26   0:00 nginx: master process /usr/sbin/nginx
www-data   56985  0.0  0.1  12936  4404 ?        S    16:26   0:00 nginx: worker process
www-data   56986  0.0  0.1  12936  4532 ?        S    16:26   0:00 nginx: worker process

root@wh:~# curl 127.1 -I -s |grep Server
Server: nginx/1.24.0 (Ubuntu)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注