1帝雇、為什么要對(duì) nginx 平滑升級(jí)
隨著 nginx 越來越流行,并且 nginx 的優(yōu)勢(shì)也越來越明顯蛉拙, nginx 的版本迭代也來時(shí)加速模式尸闸,1.9.0版本的 nginx更新了許多新功能,例如 stream 四層代理功能孕锄,伴隨著 nginx 的廣泛應(yīng)用吮廉,版本升級(jí)必然越來越快,線 上業(yè)務(wù)不能停畸肆,此時(shí) nginx 的升級(jí)就是運(yùn)維的工作了
nginx 方便地幫助我們實(shí)現(xiàn)了平滑升級(jí)宦芦。其原理簡單概括,就是: (1)在不停掉老進(jìn)程的情況下轴脐,啟動(dòng)新進(jìn)程调卑。 (2)老進(jìn)程負(fù)責(zé)處理仍然沒有處理完的請(qǐng)求,但不再接受處理請(qǐng)求大咱。 (3)新進(jìn)程接受新請(qǐng)求恬涧。 (4)老進(jìn)程處理 完所有請(qǐng)求,關(guān)閉所有連接后碴巾,停止溯捆。 這樣就很方便地實(shí)現(xiàn)了平滑升級(jí)。一般有兩種情況下需要升級(jí) nginx厦瓢,一種 是確實(shí)要升級(jí) nginx 的版本提揍,另一種是要為 nginx 添加新的模塊
2、Nginx信號(hào)簡介
主進(jìn)程支持的信號(hào)
TERM , INT : 立刻退出
QUIT : 等待工作進(jìn)程結(jié)束后再退出
KILL : 強(qiáng)制終止進(jìn)程
HUP : 重新加載配置文件煮仇,使用新的配置啟動(dòng)工作進(jìn)程劳跃,并逐步關(guān)閉舊進(jìn)程。
USR1 : 重新打開日志文件
USR2 : 啟動(dòng)新的主進(jìn)程浙垫,實(shí)現(xiàn)熱升級(jí)
WINCH : 逐步關(guān)閉工作進(jìn)程
工作進(jìn)程支持的信號(hào)
TERM , INT : 立刻退出
QUIT : 等待請(qǐng)求處理結(jié)束后再退出
USR1 : 重新打開日志文件
3售碳、nginx 平滑升級(jí)實(shí)戰(zhàn)
1、查看現(xiàn)有的 nginx 編譯參數(shù)
[root@nginx-server ~]# cd /usr/local/nginx/sbin/nginx -V
2绞呈、按照原來的編譯參數(shù)安裝 nginx 的方法進(jìn)行安裝贸人,只需要到 make,千萬不要 make install 佃声。如果make install 會(huì)將原來的配置文件覆蓋
[root@nginx-server ~]# cd /usr/local/nginx-1.16.0/
[root@nginx-server nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx -user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --errorlog-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-bodytemp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temppath=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --withhttp_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre -with-http_realip_module --with-stream --with-http_image_filter_module
[root@nginx-server nginx-1.16.0]# make
3艺智、備份原 nginx 二進(jìn)制文件
備份二進(jìn)制文件和 nginx 的配置文件(期間nginx不會(huì)停止服務(wù))
[root@nginx-server nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_$(date +%F)
4、復(fù)制新的nginx二進(jìn)制文件圾亏,進(jìn)入新的nginx源碼包
[root@nginx-server nginx-1.16.0]# cp /usr/local/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/
5十拣、測(cè)試新版本的nginx是否正常
[root@nginx-server nginx-1.16.0]# /usr/local/nginx/sbin/nginx -t
6封拧、給nginx發(fā)送平滑遷移信號(hào)(若不清楚pid路徑,請(qǐng)查看nginx配置文件
[root@nginx-server ~]# kill -USR2 `cat /var/run/nginx.pid`
7夭问、查看nginx pid泽西,會(huì)出現(xiàn)一個(gè)nginx.pid.oldbin
[root@nginx-server ~]# ll /var/run/nginx.pid*
-rw-r--r-- 1 root root 5 Jul 1 11:29 /var/run/nginx.pid
-rw-r--r-- 1 root root 5 Jul 1 09:54 /var/run/nginx.pid.oldbin
8、從容關(guān)閉舊的Nginx進(jìn)程
[root@nginx-server ~]# kill -WINCH `cat /var/run/nginx.pid.oldbin`
9缰趋、此時(shí)不重載配置啟動(dòng)舊的工作進(jìn)程
[root@nginx-server ~]# kill -HUP `cat /var/run/nginx.pid.oldbin`
10捧杉、結(jié)束工作進(jìn)程,完成此次升級(jí)
[root@nginx-server ~]# kill -QUIT `cat /var/run/nginx.pid.oldbin`
11秘血、驗(yàn)證Nginx是否升級(jí)成功
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V
4味抖、升級(jí)實(shí)驗(yàn)
1、安裝配置1.6版本的 nginx灰粮,重新開啟一臺(tái)機(jī)器
[root@localhost ~]# yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel
[root@localhost ~]# tar xzf nginx-1.6.3.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.6.3
[root@localhost nginx-1.6.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx -with-http_stub_status_module
[root@localhost nginx-1.6.3]# make && make install
[root@localhost nginx-1.6.3]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.6.3]# /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
[root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.6.3]# netstat -lntp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13989/nginx: master
2仔涩、查看版本和模塊
[root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --withhttp_stub_status_module
[root@localhost nginx-1.6.3]# echo "nginx1.6" > /usr/local/nginx/html/index.html
[root@localhost nginx-1.6.3]# yum install -y elinks
3、訪問驗(yàn)證
[root@localhost nginx-1.6.3]# elinks 10.0.105.189
nginx1.6
4粘舟、升級(jí)nginx
將 nginx 版本進(jìn)行升級(jí) 并在不影響業(yè)務(wù)的情況下添加 SSL (加密)和 pcre (正則匹配重寫)模塊
[root@localhost ~]# tar xzf nginx-1.12.2.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=ngiinx --with-http_stub_status_module --with-http_ssl_module --with-pcre
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# cd
[root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_lod
[root@localhost ~]# cp /usr/local/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin/
[root@localhost ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@localhost ~]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
[root@localhost ~]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid
[root@localhost ~]# ps aux | grep nginx
root 13989 0.0 0.0 24860 952 ? Ss 13:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 13990 0.0 0.1 25284 1720 ? S 13:55 0:00 nginx: worker process
root 16525 0.0 0.0 112708 976 pts/2 S+ 14:09 0:00 grep --color=auto nginx
5熔脂、驗(yàn)證升級(jí)成功
nginx -V