前言:
衆所周知,開源軟件的升級是快的沒得商量。Nginx也不例外,在網上鋪天蓋地的0.7、0.8版本的使用、升級、優化的方法的時候,Nginx官方5.15號已經更新到了1.3.0(開發版)。對於線上的Nginx服務器,Nginx提供了很好的升級解決方案,可以在不中斷服務的情況下,使用新版本、添加/刪除服務器模塊等,這也是我今天要寫的主題《Nginx的“安靜”升級》。
------------------------------------------------------------------------------------
步驟如下:
1)Nginx的舊版本的安裝。(線上的服務器應該先已經安裝Nginx的比較舊的版本)
詳細安裝步驟可參考我的另一篇博文:《Nginx服務器的安裝配置》 http://minitoo.blog.51cto.com/4201040/850654
2)查看一下當前的Nginx版本。
- # nginx -v
- nginx version: nginx/0.7.69
3)使用新的可執行程序替換舊的可執行程序,對於編譯安裝的Nginx,可以將新版本編譯安裝到舊版本的Nginx安裝路徑中,替換之前,您最好備份一下舊的可執行文件。
- # cp /usr/local/sbin/nginx ~/
- # tar zxf nginx-1.3.0.tar.gz
- # cd nginx-1.3.0
- # ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module
- # make && make install
4)查看一下當前的主進程號是多少,然後執行命令:kill -USR2 舊的版本的Nginx主進程號 。
- # ps -ef | grep nginx
- root 21421 1 0 20:37 ? 00:00:00 nginx: master process nginx
- nobody 24021 21421 0 20:49 ? 00:00:00 nginx: worker process
- root 28765 24076 0 22:04 pts/1 00:00:00 grep nginx
- # kill -USR2 21421
5)舊的版本Nginx的主進程將重命名它的.pid文件爲.oldbin(例如:/usr/local/nginx/logs/nginx.pid.oldbin),然後執行新的版本的Nginx可執行程序,依次啓動新的主進程和新的工作進程。
- # /usr/local/sbin/nginx
- # ps -ef | grep nginx
- root 21421 1 0 20:37 ? 00:00:00 nginx: master process nginx
- nobody 24021 21421 0 20:49 ? 00:00:00 nginx: worker process
- root 28768 21421 0 22:04 ? 00:00:00 nginx: master process nginx
- nobody 28769 28768 0 22:04 ? 00:00:00 nginx: worker process
- root 28786 24076 0 22:11 pts/1 00:00:00 grep nginx
6)此時,新、舊版本的Nginx實例會同時運行,共同處理輸入的請求。要逐步停止舊版本的Nginx實例,您必須發送WINCH信號給舊的主進程,然後,它的工作進程就將開始從容關閉。
- # kill -WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin`
7)一段時間後,舊的工作進程(worker process)處理了所有已連接的請求後退出,僅由新的工作進程來處理輸入的請求了:
- # ps -ef | grep nginx
- root 21421 1 0 20:37 ? 00:00:00 nginx: master process nginx
- root 28768 21421 0 22:04 ? 00:00:00 nginx: master process nginx
- nobody 28769 28768 0 22:04 ? 00:00:00 nginx: worker process
- root 28799 24076 0 22:15 pts/1 00:00:00 grep nginx
8)發送QUIT信號給舊的主進程,使其退出而只留下新的Nginx服務器運行。
- # kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
- # ps -ef | grep nginx
- root 28768 1 0 22:04 ? 00:00:00 nginx: master process nginx
- nobody 28769 28768 0 22:04 ? 00:00:00 nginx: worker process
- root 28808 24076 0 22:20 pts/1 00:00:00 grep nginx
9)查看當前版本號。
- # nginx -v
- nginx version: nginx/1.3.0
到這裏Nginx的“安靜”升級就成功了。
------------------------------------------------------------------
附上Nginx支持的幾種控制信號:
- * TERM,INT 快速關閉
- * QUIT 從容關閉
- * HUP 平滑重啓,重新加載配置文件
- * USR1 重新打開日誌文件,在切割日誌時用途較大
- * USR2 平滑升級可執行程序
- * WINCH 從容關閉工作進程
----------------------------------------------------------
Nginx 新的重載方式 (nginx -s reload)
Nginx 自從 0.7.53 版本之後新增了一些命令行參數,請看:
[oschina@liubc oschina]$ /opt/ngx/sbin/nginx -h
nginx version: nginx/0.8.45
Usage: nginx [-?hvVt] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/nginx-0.8.45/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
其中 -s 參數是以前版本沒有的,可用來給 Nginx 主進程發送信號
原來我們可以用 killall -s HUP nginx 或者 kill -HUP `cat /opt/ngx/logs/nginx.pid` 方法來重新加載配置,現在只需要用 nginx -s reload 命令即可。還是挺省事的。
-s 參數包含四個命令分別是 stop/quit/reopen/reload