nginx平滑升級(jí)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容