1. 程式人生 > >nginx的平滑升級

nginx的平滑升級

http 代理 entos 簡單 端口 情況 影響 需要 監聽事件

一:解釋nginx的平滑升級

隨著nginx越來越流行,並且nginx的優勢也越來越明顯,nginx的版本叠代也來時加速模式,1.9.0版本的nginx更新了許多新功能,例如stream四層代理功能,伴隨著nginx的廣泛應用,版本升級必然越來越快,線上業務不能停,此時nginx的升級就是運維的工作了

Nginx方便地幫助我們實現了平滑升級。其原理簡單概括,就是:
(1)在不停掉老進程的情況下,啟動新進程。
(2)老進程負責處理仍然沒有處理完的請求,但不再接受處理請求。
(3)新進程接受新請求。
(4)老進程處理完所有請求,關閉所有連接後,停止。
這樣就很方便地實現了平滑升級。一般有兩種情況下需要升級Nginx,一種是確實要升級Nginx的版本,另一種是要為Nginx添加新的模塊。

二.nginx平滑升級的原理:

多進程模式下的請求分配方式

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

信號的接受和處理

nginx主進程在啟動完成後進入等待狀態,負載相應各類系統消息,如SIGCHLD,SIGHUP.SIGUSR2等

nginx信號簡介

主進程支持的信號

TERM,INT:立刻退出

QUIT:等待工作進程結束在提出,

KILL:強子終止進程

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

USR1:重新打開日誌文件

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

WINCH:逐步關閉工作進程

工作進程支持的信號:

TERM,INT:立刻退出

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

USR1:重新打開日誌文件

[[email protected] ~]# yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel

[[email protected] ~]# tar zxvf nginx-1.6.0.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/nginx-1.6.0/
[[email protected]

/* */ nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

[[email protected] nginx-1.6.0]# make

[[email protected] nginx-1.6.0]# make install

[[email protected] nginx-1.6.0]# ln -s /usr/local/nginx/sbin/* /usr/sbin/
[[email protected] nginx-1.6.0]# useradd -M -s /sbin/nologin nginx
[[email protected] nginx-1.6.0]# nginx
[[email protected] nginx-1.6.0]# netstat -anput | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19008/nginx: master

目前版本是1.6.0

[[email protected] nginx-1.6.0]# nginx -v
nginx version: nginx/1.6.0

目前版本是1.6.0且只有一個模塊,該模式支持nginx狀態查詢,
[[email protected] nginx-1.6.0]# nginx -V
nginx version: nginx/1.6.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

[[email protected] nginx-1.6.0]# echo "nginx1.6.0" > /usr/local/nginx/html/index.html

[[email protected] nginx-1.6.0]# elinks 192.168.20.167

技術分享

現在我們 需要jiangnginx版本進行升級 並在不影響業務的情況下添加SSL和pcre模塊

[[email protected] ~]# tar zxvf nginx-1.11.2.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/nginx-1.11.2/
[[email protected] nginx-1.11.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=ngiinx --with-http_stub_status_module --with-http_ssl_module --with-pcre

[[email protected] nginx-1.11.2]# make

[[email protected] nginx-1.11.2]# cd
[[email protected] ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
[[email protected] ~]# cp /usr/src/nginx-1.11.2/objs/nginx /usr/local/nginx/sbin/

[[email protected] ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.old[[email protected] ~]# cp /usr/src/nginx-1.11.2/conf/nginx.conf /usr/local/nginx/conf/nginx.conf

[[email protected] ~]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
[[email protected] ~]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid

[[email protected] ~]# ps aux | grep nginx
root 19008 0.0 0.0 24324 944 ? Ss 14:07 0:00 nginx: master process nginx
nginx 19009 0.0 0.1 26832 1744 ? S 14:07 0:00 nginx: worker process
root 53194 0.0 0.0 112660 976 pts/0 R+ 14:36 0:00 grep --color=auto ngin

驗證nginx是否升級成功

技術分享

nginx的平滑升級