若是要對當前的Nginx服務器進行版本升級,應用新模塊,若是用戶訪問量比較大的時候,若是須要在不影響客戶的狀況下進行升級的話,這時候就得考慮平滑升級了。node
平滑升級的過程,Nginx服務器接受到USR2信號後,首先將舊的nginx.pid文件添加後綴.oldbin,變爲nginx.pid.oldbin文件,而後執行新版本的Nginx服務器的二進制的文件啓動服務,這個時候須要提早將編譯好的新版本的二進制實現複製到sbin文件夾中。若是新的服務啓動成功,系統中將有新舊兩個Nginx服務共同提供Web服務。以後,須要向舊的Nginx服務器進程發送WINCH信號,使舊的Nginx服務平滑中止,並刪除nginx.pid.oldbin文件。在發送WINCH信號以前,若是發現有什麼錯誤,能夠隨時中止新的Nginx服務。nginx
這裏以前已經編譯安裝了naginx1.8.0。編譯的時候只指定了按照路徑 --prefix=/nginx/。這裏演示從nginx1.8.0升級到nginx 1.9.6
web
[root@c7node1 ~]# /nginx/sbin/nginx -V nginx version: nginx/1.8.0 built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) configure arguments: --prefix=/nginx/ [root@c7node1 ~]# ss -tnl LISTEN 0 12 *:80 *:*
這裏爲了測試升級過程當中Web沒有中斷,提供一個腳本讓其每秒去請求頁面服務。並紀錄到日誌中vim
[root@c7node1 test]# vim http.sh #!/bin/bash # URL="http://192.168.0.66/" HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` if [ $HTTP_CODE == 200 ];then echo Success.$HTTP_CODE >> /tmp/ping.http else echo Failure.404 >> /tmp/ping.http fi #請求http服務,把請求成功和失敗發送到ping.http文件中 [root@c7node1 test]# vim cron.sh #!/bin/bash # while true;do /test/http.sh sleep 1 done #爲cron服務提供每秒執行服務 [root@c7node1 test]# crontab -e * * * * * /bin/bash /test/cron.sh #定義每分鐘執行cron.sh任務
下載編譯nginx-1.9.6,並進行平滑升級,爲了實現Nginx服務器的平滑升級,新的服務安裝路徑應該和舊的服務安裝路徑保持一致。若是由於某種緣由不一致,須要把舊的服務的安裝路徑更改成新的服務的安裝路徑,修改方法爲(nginx -p NewInstallPath)bash
[root@c7node1 ~]# wget http://nginx.org/download/nginx-1.9.6.tar.gz [root@c7node1 ~]# tar xf nginx-1.9.6.tar.gz [root@c7node1 ~]# cd nginx-1.9.6 [root@c7node1 nginx-1.9.6]# ./configure --prefix=/nginx/ [root@c7node1 nginx-1.9.6]# make [root@c7node1 mv /nginx/sbin/nginx{,.bak} #這裏備份舊的nginx,一方面是等一下須要把新的nginx複製過來,另外一方面的若是新的nginx有問題,還能夠進行恢復 [root@c7node1 nginx-1.9.6]# cp objs/nginx /nginx/sbin/ #把編譯好的新的nginx文件服務到指定的安裝目錄中 [root@c7node1 sbin]# /nginx/sbin/nginx -t nginx: the configuration file /nginx//conf/nginx.conf syntax is ok nginx: configuration file /nginx//conf/nginx.conf test is successful #測試新編譯的nginx檢測配置文件是否正常 [root@c7node1 nginx-1.9.6]# kill -USR2 `cat /nginx/logs/nginx.pid` #使用新版本的Nginx文件啓動服務,以後平緩中止原有Nginx進程 [root@c7node1 nginx-1.9.6]# cd /nginx/logs/ access.log error.log nginx.pid nginx.pid.oldbin #能夠看到這裏自動把以前的nginx.pid命名爲nginx.pid.oldbin,併爲新的nginx生成nginx.pid [root@c7node1 logs]# kill -WINCH `cat /nginx/logs/nginx.pid.oldbin` #平緩中止舊服務的worker process [root@c7node1 logs]# nginx -V nginx version: nginx/1.9.6 built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) configure arguments: --prefix=/nginx/ [root@c7node1 logs]# ps -aux |grep nginx root 990 0.0 0.2 24316 1880 ? S 11:25 0:00 nginx: master process nginx nobody 992 0.9 0.2 24724 1712 ? S 11:25 0:03 nginx: worker process root 2357 0.0 0.1 24308 896 ? Ss 11:20 0:00 nginx: master process nginx #能夠看到有兩個master process進程
測試結果服務器
[root@c7node1 ~]# sort /tmp/ping.http | uniq -c 32822 Success.200 #能夠看到服務並無停止
此部份內容參考《Nginx 高性能web服務器詳解》 網絡
快速中止Nginx服務:快速中止是指當即中止當前Nginx服務正在處理的全部網絡請求,立刻丟棄鏈接,中止工做curl
[root@c7node1 ~]# kill -TERM `cat /nginx/logs/nginx.pid` [root@c7node1 ~]# kill -INT `cat /nginx/logs/nginx.pid`
平緩中止Nginx服務:平緩中止是指容許Nginx服務將當前正在處理的網絡請求處理完成,但再也不接受新的請求,以後關閉鏈接,中止工做ide
[root@c7node1 ~]# kill -QUIT `cat /nginx/logs/nginx.pid`
平緩重啓Nginx服務:Nginx服務進程接受到信號後,首先讀取新的Nginx配置文件,若是配置語法正確,則啓動新的Nginx服務,而後平緩關閉舊的服務進程,若是新的Nginx配置文件有問題,將顯示錯誤,仍然使用舊的Nginx進程提供服務性能
[root@c7node1 ~]# kill -HUP `cat /nginx/logs/nginx.pid`
日誌切割:從新打開日誌文件,經常使用於日誌切割
[root@c7node1 ~]# kill -USR1 `cat /nginx/logs/nginx.pid`
平緩升級Nginx服務:使用新版本的Nginx文件啓動服務,以後平緩中止原有的Nginx進程
[root@c7node1 ~]# kill -USR2 `cat /nginx/logs/nginx.pid`
平緩中止worker process:用於Nginx服務平緩升級
[root@c7node1 ~]# kill -WINCH `cat /nginx/logs/nginx.pid`