Nginx平滑升級



一、nginx平滑升級概述
隨着網站並發訪問量越來越高,nginx web 服務器也越來越流行,nginx 版本換代越來越頻繁,1.16.2版本的nginx更新了許多新功能,生產環境中版本升級必然的,但是線上業務不能停,此時nginx的升級就是運維的重要工作了。

二、nginx平滑升級原理
多進程模式下的請求分配方式

 

Nginx默認工作在多進程模式下,即主進程(master process)啟動后完成配置加載和端口綁定等動作,fork出指定數量的工作進程(worker process),這些子進程會持有監聽端口的文件描述符(fd),並通過在該描述符上添加監聽事件來接受連接(accept)。

 

信號的接收和處理

Nginx主進程在啟動完成后會進入等待狀態,負責響應各類系統消息,如SIGCHLD、SIGHUP、SIGUSR2等。

 

Nginx信號簡介

 

主進程支持的信號

TERM, INT: 立刻退出

QUIT: 等待工作進程結束后再退出

KILL: 強制終止進程

HUP: 重新加載配置文件,使用新的配置啟動工作進程,並逐步關閉舊進程。

USR1: 重新打開日志文件

USR2: 啟動新的主進程,實現熱升級

WINCH: 逐步關閉工作進程

 

 

工作進程支持的信號

TERM, INT: 立刻退出

QUIT: 等待請求處理結束后再退出

USR1: 重新打開日志文件

 

三、nginx平滑升級實戰

1.查看舊版nginx的編譯參數

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module

 

2.編譯新版本Nginx源碼包,安裝路徑需與舊版一致,注意:不要執行make install

[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.16.0/
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module&&make

 

3.備份二進制文件,用新版本的替換
[root@localhost nginx-1.15.9]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

[root@localhost nginx-1.15.9]# cp objs/nginx /usr/local/nginx/sbin/

 

4.確保配置文件無報錯
[root@localhost nginx-1.15.9]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

5.發送USR2信號
向主進程(master)發送USR2信號,Nginx會啟動一個新版本的master進程和對應工作進程,和舊版一起處理請求

[root@localhost ~]# ps aux | grep nginx
root 16396 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21993 0.0 0.2 112724 996 pts/1 S+ 20:42 0:00 grep --color=auto nginx

[root@localhost ~]# kill -USR2 16396

 

[root@localhost ~]# ps aux | grep nginx
root 16396 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
root 21997 0.0 0.2 112724 996 pts/1 R+ 20:44 0:00 grep --color=auto nginx

 

6.發送WINCH信號
向舊的Nginx主進程(master)發送WINCH信號,它會逐步關閉自己的工作進程(主進程不退出),這時所有請求都會由新版Nginx處理

[root@localhost ~]# kill -WINCH 16396

[root@localhost ~]# ps aux | grep nginx
root 16396 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process

 

注意:回滾步驟,發送HUP信號

如果這時需要回退繼續使用舊版本,可向舊的Nginx主進程發送HUP信號,它會重新啟動工作進程, 仍使用舊版配置文件。然后可以將新版Nginx進程殺死(使用QUIT、TERM、或者KILL)

[root@localhost ~]# kill -HUP 16396

 

7.發送QUIT信號
升級完畢,可向舊的Nginx主進程(master)發送(QUIT、TERM、或者KILL)信號,使舊的主進程退出

[root@localhost ~]# kill -QUIT 16396

[root@localhost ~]# ps aux | grep nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process

 

8.驗證nginx版本號,並訪問測試

[root@localhost ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.16.0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM