一、解釋nginx的平滑升級
隨着nginx越來越流行使用,並且nginx的優勢也越來越明顯,nginx的版本迭代也開始了加速模式,1.9.0版本的nginx更新了許多新功能,例如stream四層代理功能。伴隨着nginx的廣泛應用,版本升級必然是越來越快的,線上業務不能停,此時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平滑升級實戰
一:查看能不能通Welcome to nginx頁面
[root@location ~]#rpm -q httpd
[root@location ~]#yum -y install gcc gcc-c++ make zlib-devel pcre-devel elinks
[root@location ~]#ll nginx-*
[root@location ~]#useradd -M -s /sbin/nologin nginx
[root@location ~]#tail -l /etc/passwd;tail -l /etc/group
[root@location ~]#elinks --dump http://location
Welcome to nginx
二:查看舊版本nginx的編譯參數
[root@iZwz994oywnbrdsnn4hrkqZ /]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-stream --with-http_gzip_static_module
三:編譯新版本Nginx源碼包,安裝路徑必須與舊版本一致,且不能執行make install(本次用1.16.0的版本做實驗)
3.1:為了實驗的成功先殺死nginx進程
[root@localhost ~]# killall -9 nginx
[root@localhost ~]# nginx
[root@localhost ~]#rz -E(此處上傳新源碼包,這里用1.15.9代替做實驗)
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.14.2.tar.gz original-ks.cfg 模板 圖片 下載 桌面
initial-setup-ks.cfg nginx-1.16.0.tar.gz 公共 視頻 文檔 音樂
3.2:先解壓1.15.9版本
[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 && make
3.3:備份二進制文件用新版本代替
[root@localhost nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old //將舊版本的nginx改名並備份
[root@localhost nginx-1.16.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@localhost nginx-1.16.0]# ls objs/
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.16.0]# cp objs/nginx /usr/local/nginx/sbin
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? yes
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.16.0]# ll /usr/local/nginx/sbin
total 3772
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.16.0]# cd /usr/src/nginx-1.14.2/
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.14.2]# mv objs/nginx nginx.old
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.14.2]# cp nginx.old /usr/local/nginx/sbin/
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.14.2]# ll /usr/local/nginx/sbin/
total 8044
-rwxr-xr-x 1 root root 3858728 Feb 6 15:42 nginx #新版本
-rwxr-xr-x 1 root root 4373160 Feb 6 15:44 nginx.old #舊版本
[root@localhost nginx-1.16.0]# nginx -t //啟動nginx,讓新的配置文件加載舊的配置文件看兼容不兼容
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
四:進行信號處理(發送USR2信號)
向主進程(master)發送USR2信號。Nginx會啟動一個新版本的master進程和對應的工作進程
和舊版本一起處理請求
[root@localhost nginx-1.16.0]# cd
[root@iZwz994oywnbrdsnn4hrkqZ ~]# ps -aux | grep nginx
root 19604 0.0 0.1 20560 1232 ? Ss 15:45 0:00 nginx: master process nginx #舊進程
nginx 19607 0.0 0.5 24812 5124 ? S 15:45 0:00 nginx: worker process #舊進程
root 19609 0.0 0.0 112708 988 pts/0 R+ 15:45 0:00 grep --color=auto nginx
root 71140 0.0 0.0 112724 992 pts/1 S+ 20:00 0:00 grep --color=auto nginx
[root@localhost ~]# kill -USR2 68104 //殺掉舊進程主程序
[root@iZwz994oywnbrdsnn4hrkqZ ~]# nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module