[root@suian src]# wget https://nginx.org/download/nginx-1.25.1.tar.gz
上次发布的文章是使用编译方式安装的nginx-1.24.0版本,本章节记录了平滑升级到nginx-1.25.1版本。
在生产环境中尽量使用平滑升级,操作没问题不需要关闭服务,从而达到免停升级服务。
[root@suian src]# ls
nginx-1.24.0 nginx-1.25.1 nginx-1.25.1.tar.gz
[root@suian src]# cd nginx-1.25.1/
#目前版本还是1.24.0
[root@suian nginx-1.25.1]# /apps/nginx/sbin/nginx -v
nginx version: nginx/1.24.0
[root@suian nginx-1.25.1]# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-18) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@suian nginx-1.25.1]# vim install.sh
#!/bin/bash
./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@suian nginx-1.25.1]# bash install.sh
#只需要执行make,不需要执行make install
[root@suian nginx-1.25.1]# make
~
~
[root@suian nginx-1.25.1]# objs/nginx -v
nginx version: nginx/1.25.1
[root@suian nginx-1.25.1]# ll objs/nginx /apps/nginx/sbin/nginx
-rwxr-xr-x 1 nginx nginx 7639864 7月 29 10:15 /apps/nginx/sbin/nginx
-rwxr-xr-x 1 root root 7654480 7月 29 15:12 objs/nginx
[root@suian nginx-1.25.1]#cp /apps/nginx/sbin/nginx /opt/nginx.old
#把新版本的nginx命令复制过去覆盖到旧版本程序文件,注意:需要加 -f 选项强制覆盖,否则会提示Text file busy
[root@suian nginx-1.25.1]#cp -f ./objs/nginx /apps/nginx/sbin/
[root@suian nginx-1.25.1]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@suian nginx-1.25.1]# kill -USR2 `cat /apps/nginx/logs/nginx.pid`
注意:在Nginx-1.22.1版中如果看不到下面新版进程,需要重新使用service方式重新启动nginx服务再发 送USR2信号
[root@suian nginx-1.25.1]#ps auxf|grep nginx
root 12018 0.0 0.0 12112 1092 pts/0 S+ 17:32 0:00 |
\_ grep --color=auto nginx
root 8814 0.0 0.2 42460 3760 ? Ss 16:58 0:00 nginx: master
process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 8957 0.0 0.2 77172 4724 ? S 17:23 0:00 \_ nginx:
worker process
nginx 8958 0.0 0.2 77172 4724 ? S 17:23 0:00 \_ nginx:
worker process
root 12014 0.0 0.3 42448 5512 ? S 17:32 0:00 \_ nginx:
master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 12015 0.0 0.2 77192 4904 ? S 17:32 0:00 \_
nginx: worker process
nginx 12016 0.0 0.2 77192 4908 ? S 17:32 0:00 \_
nginx: worker process
使用另外一台机器查看仍是旧版程序
root@ubuntu:~# curl http://10.0.0.102 -I
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sat, 29 Jul 2023 07:31:36 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 29 Jul 2023 02:15:18 GMT
Connection: keep-alive
ETag: "64c47636-267"
Accept-Ranges: bytes
向原老的Nginx主进程发送WINCH信号,它会平滑关闭老的工作进程(主进程不退出),这时所有新请求都会 由新版Nginx处理
[root@suian nginx-1.25.1]#kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
如果旧版worker进程有用户的旧的请求,会一直等待处理完后才会关闭,即平滑关闭
如果有新请求,由新版本提供服务
root@ubuntu:~# curl http://10.0.0.102 -I
HTTP/1.1 200 OK
Server: nginx/1.25.1
Date: Sat, 29 Jul 2023 07:33:17 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 29 Jul 2023 02:15:18 GMT
Connection: keep-alive
ETag: "64c47636-267"
Accept-Ranges: bytes
[root@suian nginx-1.25.1]# nginx -v
nginx version: nginx/1.25.1
[root@suian nginx-1.25.1]# nginx -V
nginx version: nginx/1.25.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-18) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
[root@suian nginx-1.25.1]#kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
[root@suian nginx-1.25.1]#pstree -p |grep nginx
|-nginx(8814)-+-nginx(12014)-+-nginx(12015)
| | `-nginx(12016)
| |-nginx(12090)
| `-nginx(12091)
[root@suian nginx-1.25.1]#kill -QUIT `cat /apps/nginx/logs/nginx.pid`
#恢复旧版的文件
[root@suian nginx-1.25.1]#mv /opt/nginx.old /apps/nginx/sbin/
mv: overwrite '/apps/nginx/sbin/nginx'? y
官方源码包下载地址
nginx: download
有问题的小伙伴可以评论留言或者私信我都可以帮忙答疑解惑哦。