簡介
有時候因為nginx缺少某個模塊芯侥,或者想升級一下版本羡洁,但是你nginx上有運載著一些服務(wù),不能掛掉,這樣掌握nginx的平滑升級就顯得至關(guān)重要了
nginx信號檢測
主進程支持的信號
TERM,INT:立刻退出
QUIT:等待工作進程結(jié)束在提出社证,
KILL:強子終止進程
HUP:重新加載配置文件,使用新的的配置啟動工作進程评凝,并逐步關(guān)閉日進程
USR1:重新打開日志文件
USR2:啟動新的主進程追葡,實現(xiàn)熱升級
WINCH:逐步關(guān)閉工作進程
工作進程支持的信號:
TERM,INT:立刻退出
QUIT:等待請求處理結(jié)束后再退出
USR1:重新打開日志文件
原理
- 在不停掉老進程的情況下肥哎,啟動新進程辽俗。
- 老進程負(fù)責(zé)處理仍然沒有處理完的請求,但不再接受處理請求篡诽。
- 新進程接受新請求崖飘。
- 老進程處理完所有請求,關(guān)閉所有連接后杈女,停止朱浴。
平滑升級nginx
先查看我老nginx的版本
? /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.42 --with-stream
我要把它升級到nginx1.14.0
下載1.14.0nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
./configure --prefix=/home/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.42 --with-stream #--prefix的位置必須和老版本的位置一樣
make #記住這里只能make,不能make install达椰,如果make install翰蠢,會導(dǎo)致原先的配置被覆蓋掉
然后備份原先的二進制啟動文件
mv /home/nginx/sbin/nginx /home/nginx/sbin/nginx_old
cp /usr/src/nginx-1.11.2/objs/nginx /usr/local/nginx/sbin/ #把編譯好的新的啟動文件復(fù)制進來
首先查看進程
ps -ef |grep nginx
root 10111 1 0 10:17 ? 00:00:00 nginx: master process ./nginx
nginx 10112 10111 0 10:17 ? 00:00:00 nginx: worker process
root 16669 9963 0 10:54 pts/0 00:00:00 grep --color=auto nginx
然后加載新啟動文件,實現(xiàn)熱升級
kill -USR2 `cat home/nginx/logs/nginx.pid` #發(fā)送USR2信號給nginx master 進程(nginx服務(wù)接收到USR2信號后啰劲,首先會將舊的nginx.pid文件添加后綴.oldbin梁沧,變?yōu)閚ginx.pid.oldbin,然后執(zhí)行新版本的二進制文件啟動服務(wù),如果新的服務(wù)啟動成功蝇裤,系統(tǒng)中將有新舊兩個Nginx服務(wù)共同提供web服務(wù))
ps -ef |grep nginx
root 10111 1 0 10:17 ? 00:00:00 nginx: master process ./nginx
nginx 10112 10111 0 10:17 ? 00:00:00 nginx: worker process
root 16671 10111 0 10:57 ? 00:00:00 nginx: master process ./nginx
nginx 16672 16671 0 10:57 ? 00:00:00 nginx: worker process
root 16699 9963 0 10:57 pts/0 00:00:00 grep --color=auto nginx
你會發(fā)現(xiàn)有兩個nginx進程廷支,舊的nginx進程還在
通過發(fā)送WINCH信號(平緩?fù)V箇orker process)和QUIT信號(平緩?fù)V筃ginx服務(wù))停止舊的Nginx服務(wù)進程频鉴,逐步關(guān)閉舊進程和舊進程的工作進程
kill -WINCH `cat /home/nginx/logs/nginx.pid.oldbin`
kill -QUIT `cat /home/nginx/logs/nginx.pid.oldbin`
在重新查看進程
ps -ef |grep nginx
root 16671 1 0 10:57 ? 00:00:00 nginx: master process ./nginx
nginx 16672 16671 0 10:57 ? 00:00:00 nginx: worker process
root 16707 9963 0 10:59 pts/0 00:00:00 grep --color=auto nginx
這時候在重新查看版本信息
./nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/home/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.42 --with-stream
發(fā)現(xiàn)老的nginx進程已經(jīng)關(guān)閉了,版本也已經(jīng)升級到了nginx1.14.0了恋拍,這就實現(xiàn)了一個業(yè)務(wù)不中斷的熱升級的過程