Red Hat. System nginx smooth upgrade version

Install the latest version of nginx-1.25.1.tar.gz package

[root@suian src]# wget https://nginx.org/download/nginx-1.25.1.tar.gz

The last article published was about nginx-1.24.0 version installed using compilation method. This chapter records the smooth upgrade to nginx-1.25.1 version.

Try to use smooth upgrade in the production environment. There is no need to shut down the service without any problem, so as to achieve a non-stop upgrade service.

[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

Start compiling the new version---(Because the code here is too long, I abbreviated the code into the script)

[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

Compilation completed and installation started

#只需要执行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

Now you can see that there are two nginx versions of the command files

[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

Now backup the old version of nginx command

[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/ 

Detect the new version and configuration file syntax and tolerability, execute kill to kill the process

 [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`

You can see two masters. The new master is a child process of the old master and generates a new worker process.

Note: If you cannot see the following new version of the process in Nginx-1.22.1 version, you need to use the service mode to restart the nginx service and then send the USR2 signal

[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

Use another machine to view the old version of the program

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

Close the old nginx worker process first, without closing the old nginx main process to facilitate rollback.

Send the WINCH signal to the old Nginx main process, which will smoothly shut down the old worker process (the main process will not exit). At this time, all new requests will be processed by the new version of Nginx.

[root@suian nginx-1.25.1]#kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`

If the old version of the worker process has an old request from the user, it will wait until it is processed before shutting down, that is, it will shut down smoothly.

If there are new requests, they will be served by the new version

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

If you find that there is a problem with the new version and need to be rolled back, you can send a HUP signal to restart the old version of the worker.

[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)

Finally, close the new version of master and worker. If the above HUP signal is not executed, the QUIT signal in this step can also restart the old version of the worker process.

[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

OK, this chapter is over.

 Official source code package download address

nginx: download

If you have any questions, you can leave a comment or send me a private message and I can help answer your questions.

Guess you like

Origin blog.csdn.net/Qx_cd/article/details/131995845