Nginx平滑升級

Nginx/1.5.7 升級到 nginx/1.10.2

當須要將正在運行的Nginx升級、添加或刪除服務模塊時,能夠在不中斷服務的狀況下,更新升級,從新編譯Nginx替換舊版本。
[root@ ~]# /usr/local/nginx/sbin/nginx  -V
nginx version: nginx/1.5.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_auth_request_module --add-module=/data/src/nginx-upstream-fair-master
編譯 nginx/1.10.2,具體的編譯選項根據業務須要來,這裏只是作版本升級。
[root@ src]# tar xf nginx-1.10.2.tar.gz
[root@ src]# cd nginx-1.10.2
[root@ src]# ./configure  --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_auth_request_module --add-module=/data/src/nginx-upstream-fair-master 
[root@ src]# make && make install
編譯完後,舊版的二進制文件已經被替換爲新版的。這時讓新的進程也啓動:
[root@ nginx-1.10.2]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` && sleep 1s

此時,系統中存在新舊兩類nginx進程nginx

[root@ nginx-1.10.2]#lsof -i:80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   16717 root    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   16718  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   16719  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   16720  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   16721  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19567 root    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19568  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19569  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19570  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19571  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
舊版的nginxpid文件會被命名爲nginx.pid.oldbin,這時候從容關閉舊的nginx 子進程,再關閉舊ningx主進程
[root@ nginx-1.10.2]# kill -WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin`          #關閉舊的子進程                 
[root@ nginx-1.10.2]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`              #關閉舊ningx主進程
[root@ nginx-1.10.2]# lsof -i:80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   19567 root    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19568  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19569  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19570  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
nginx   19571  www    9u  IPv4  30087      0t0  TCP *:http (LISTEN)
回滾

在執行關閉舊子進程前能夠停留幾分鐘,或者在執行關閉舊子進程後停留幾分鐘,觀察新進程是否正常。若是有異常能夠進行回滾shell

[root@ nginx-1.10.2]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid.oldbin`  #從新啓動舊子進程
[root@ nginx-1.10.2]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`           #退出新ningx主進程
相關文章
相關標籤/搜索