Nginx平滑升級的詳細操作方法
阿新 • • 發佈:2021-09-17
轉至:https://blog.csdn.net/midion9/article/details/49684325
一、平滑升級概述
Nginx方便地幫助我們實現了平滑升級。其原理簡單概括,就是:
(1)在不停掉老程序的情況下,啟動新程序。
(2)老程序負責處理仍然沒有處理完的請求,但不再接受處理請求。
(3)新程序接受新請求。
(4)老程序處理完所有請求,關閉所有連線後,停止。
這樣就很方便地實現了平滑升級。一般有兩種情況下需要升級Nginx,一種是確實要升級Nginx的版本,另一種是要為Nginx新增新的模組。
二.、升級過程
具體的操作也很簡單,如下:
(0)檢視當前版本
在存放Nginx的可執行檔案的目錄下輸入:
./nginx -V
(1)下載新的Nginx版本並編譯。
wget nginx-1.0.11.tar.gz tar zxvf nginx-1.0.11.tar.gz cd nginx-1.0.11 ./configure --add-module=/customized_module_0 --add-module=/customized_module_1 make
注意不要進行make install
(2)備份老版本的可執行檔案
cd /usr/local/nginx/sbin sudo cp nginx nginx.old
(3)修改配置檔案
如果有必要的話,進行配置檔案的修改。
(4)拷貝新的可執行檔案
sudo cp /home/michael/tmp/nginx-1.0.11/objs/nginx /usr/local/nginx/sbin/
(5)升級
cd /home/michael/tmp/nginx-1.0.11 make upgrade
(6)清理多餘檔案
rm -rf /home/michael/tmp/nginx-1.0.11
(7)檢視Nginx版本
cd /usr/local/nginx/sbin
./nginx -V
三、觀察程序變化
在我的機器上可以觀察到,我配置的是10個worker程序,啟動後觀察到
root 6241 10419 0 10:51 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody6242 6241 2 10:51 ? 00:00:00 nginx: worker process nobody 6243 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6244 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6245 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6246 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6247 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6248 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6249 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6250 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6251 6241 1 10:51 ? 00:00:00 nginx: worker process nobody 6252 6241 0 10:51 ? 00:00:00 nginx: cache manager process nobody 6253 6241 0 10:51 ? 00:00:00 nginx: cache loader process luming 6310 25051 0 10:51 pts/1 00:00:00 grep --color=auto nginx nobody 7995 10419 0 Jan12 ? 00:20:37 nginx: worker process is shutting down nobody 7996 10419 0 Jan12 ? 00:20:11 nginx: worker process is shutting down nobody 7998 10419 0 Jan12 ? 00:20:04 nginx: worker process is shutting down nobody 8003 10419 0 Jan12 ? 00:20:12 nginx: worker process is shutting down root 10419 1 0 Jan08 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
可見新的程序有1個master和10個worker,另外還有1個老的master(可以從時間上看出)和4個worker(其他6個老的worker已經處理完所有連線而shutdown了)。還有一個loader程序。過幾秒種可以看到worker只有兩個了:
root 6241 10419 0 10:51 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 6242 6241 1 10:51 ? 00:00:00 nginx: worker process nobody 6243 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6244 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6245 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6246 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6247 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6248 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6249 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6250 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6251 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6252 6241 0 10:51 ? 00:00:00 nginx: cache manager process nobody 6253 6241 0 10:51 ? 00:00:00 nginx: cache loader process luming 6430 25051 0 10:51 pts/1 00:00:00 grep --color=auto nginx nobody 7996 10419 0 Jan12 ? 00:20:11 nginx: worker process is shutting down nobody 8003 10419 0 Jan12 ? 00:20:12 nginx: worker process is shutting down root 10419 1 0 Jan08 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
再過一小會兒觀察:
root 6241 1 0 10:51 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 6242 6241 0 10:51 ? 00:00:01 nginx: worker process nobody 6243 6241 0 10:51 ? 00:00:01 nginx: worker process nobody 6244 6241 0 10:51 ? 00:00:01 nginx: worker process nobody 6245 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6246 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6247 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6248 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6249 6241 0 10:51 ? 00:00:00 nginx: worker process nobody 6250 6241 0 10:51 ? 00:00:01 nginx: worker process nobody 6251 6241 0 10:51 ? 00:00:02 nginx: worker process nobody 6252 6241 0 10:51 ? 00:00:00 nginx: cache manager process luming 8680 25051 0 10:56 pts/1 00:00:00 grep --color=auto nginx
Congratulations! You can upgrade your Nginx server gracefully.