Nginx热升级回滚实操

Nginx 1.20.0<—>1.20.1升级回滚实操

准备好待升级的Nginx执行文件并对原Nginx执行文件做好备份,本例中nginx/nginx.old是1.20.0,nginx.new是1.20.1。

[root@test01 sbin]# ll
total 23868
lrwxrwxrwx. 1 root root 32 May 29 20:30 n -> /root/openresty/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 8144640 Nov 9 10:32 nginx
-rwxr-xr-x. 1 root root 8143536 Nov 5 15:06 nginx.new
-rwxr-xr-x. 1 root root 8144640 Nov 5 16:06 nginx.old
[root@test01 sbin]# nginx -v
nginx version: nginx/1.20.0

启动nginx1.20.0并测试工作正常

[root@test01 sbin]# nginx
[root@test01 sbin]# curl test -I
HTTP/1.1 200 OK
Server: nginx/1.20.0
Date: Tue, 09 Nov 2021 02:32:42 GMT
Content-Type: text/html
Content-Length: 620
Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT
Connection: keep-alive
ETag: “60d2d558-26c”
Accept-Ranges: bytes

[root@test01 sbin]# ps -ef | grep nginx
root 6169 1 0 10:32 ? 00:00:00 nginx: master process nginx
root 6170 6169 0 10:32 ? 00:00:00 nginx: worker process
root 6183 4425 0 10:32 pts/0 00:00:00 grep --color=auto nginx

更新nginx执行文件

[root@test01 sbin]# cp -f nginx.new nginx
cp: overwrite ‘nginx’? y
[root@test01 sbin]# ll
total 23868
lrwxrwxrwx. 1 root root 32 May 29 20:30 n -> /root/openresty/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 8143536 Nov 9 10:33 nginx
-rwxr-xr-x. 1 root root 8143536 Nov 5 15:06 nginx.new
-rwxr-xr-x. 1 root root 8144640 Nov 5 16:06 nginx.old

[root@test01 sbin]# nginx -v
nginx version: nginx/1.20.1

对老master进程发送USR2信号,使老的worker进程不再响应请求,注:执行完此命令后还会自动起新的master和worker进程。

[root@test01 sbin]# kill -USR2 6169
[root@test01 sbin]# ps -ef | grep nginx
root 6169 1 0 10:32 ? 00:00:00 nginx: master process nginx
root 6170 6169 0 10:32 ? 00:00:00 nginx: worker process
root 6204 6169 0 10:33 ? 00:00:00 nginx: master process nginx
root 6205 6204 0 10:33 ? 00:00:00 nginx: worker process
root 6209 4425 0 10:33 pts/0 00:00:00 grep --color=auto nginx

对老master进程发送WINCH信号关闭老的worker进程,热升级结束。注:此时老master进程仍在运行,为回滚提供了可能。

[root@test01 sbin]# kill -WINCH 6169
[root@test01 sbin]# ps -ef | grep nginx
root 6169 1 0 10:32 ? 00:00:00 nginx: master process nginx
root 6204 6169 0 10:33 ? 00:00:00 nginx: master process nginx
root 6205 6204 0 10:33 ? 00:00:00 nginx: worker process
root 6214 4425 0 10:34 pts/0 00:00:00 grep --color=auto nginx

回滚,如升级有问题需要回滚,执行以下步骤

对老master发送HUP信号,拉起老的worker进程。
注:HUP等同于SIGHUP, nginx -s reload,但此时nginx二进制文件已更新,故不能直接reload。

[root@test01 sbin]# kill -HUP 6169
[root@test01 sbin]# ps -ef | grep nginx
root 6169 1 0 10:32 ? 00:00:00 nginx: master process nginx
root 6204 6169 0 10:33 ? 00:00:00 nginx: master process nginx
root 6205 6204 0 10:33 ? 00:00:00 nginx: worker process
root 6218 6169 0 10:34 ? 00:00:00 nginx: worker process
root 6220 4425 0 10:34 pts/0 00:00:00 grep --color=auto nginx

用nginx -s quit关闭新的nginx

[root@test01 sbin]# nginx -s quit
[root@test01 sbin]# ps -ef | grep nginx
root 6169 1 0 10:32 ? 00:00:00 nginx: master process nginx
root 6218 6169 0 10:34 ? 00:00:00 nginx: worker process
root 6240 4425 0 10:35 pts/0 00:00:00 grep --color=auto nginx

[root@test01 sbin]# ll …/logs
total 648
-rw-r–r--. 1 root root 181 Nov 9 10:32 access.log
-rw-r–r--. 1 root root 323945 Nov 8 22:33 access.log.old
-rw-r–r--. 1 root root 49711 Nov 9 10:35 error.log
-rw-r–r--. 1 root root 59543 Jun 6 16:44 myerror.log
-rw-r–r--. 1 root root 5 Nov 9 10:32 nginx.pid
-rw-r–r--. 1 root root 4986 May 29 20:30 rewrite_error.log
-rw-r–r--. 1 root root 462 Jun 25 12:12 stream_access.log
-rw-r–r--. 1 root root 85093 Jul 16 16:03 stream.log
-rw-r–r--. 1 root root 98286 May 29 20:30 test.access.log
-rw-r–r--. 1 root root 3846 Jun 3 14:53 vartest.log

[root@test01 sbin]# cat …/logs/nginx.pid
6169
[root@test01 sbin]# ll
total 23868
lrwxrwxrwx. 1 root root 32 May 29 20:30 n -> /root/openresty/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 8143536 Nov 9 10:33 nginx
-rwxr-xr-x. 1 root root 8143536 Nov 5 15:06 nginx.new
-rwxr-xr-x. 1 root root 8144640 Nov 5 16:06 nginx.old
[root@test01 sbin]# nginx -v
nginx version: nginx/1.20.1
[root@test01 sbin]# curl test -I
HTTP/1.1 200 OK
Server: nginx/1.20.0
Date: Tue, 09 Nov 2021 02:36:13 GMT
Content-Type: text/html
Content-Length: 620
Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT
Connection: keep-alive
ETag: “60d2d558-26c”
Accept-Ranges: bytes

注:此时运行的nginx版本是1.20.0,但nginx二进制执行文件还是1.20.1。

[root@test01 sbin]# nginx -v
nginx version: nginx/1.20.1
[root@test01 sbin]#

为避免误解,应再次cp还原nginx二进制执行文件。

[root@test01 sbin]# cp -f nginx.old nginx
cp: overwrite ‘nginx’? y

[root@test01 sbin]# nginx -v
nginx version: nginx/1.20.0
[root@test01 sbin]#

你可能感兴趣的:(Nginx,nginx,服务器,linux)