Nginx筆記系列(3)——Nginx的平滑升級

什麼是平滑升級?
html

對於一個正在運行中nginx服務器,若是咱們須要對其進行升級到高版本的nginx。也許你會直接把nginx中止,而後完成升級操做,以後再次啓動nginx。這種方式的缺點是顯而易見的,一些正在處理的請求就會直接丟失了。(有點像狗血片裏的XXX:「這日子無法兒過了!」刷東西。。。)強制升級,服務器上運行的進程會受到影響。所以,才須要在不中止nginx的狀況下對nginx進行平滑升級。nginx

平滑升級:不會停掉在運行着的進程,這些進程會繼續處理請求,但不會再接收請求,這些老進程處理完正在處理的這些請求以後,會中止。(有點像武俠片裏的XXX:「等我幹完手上這幾件事情以後就金盆洗手」)
shell

在平滑升級中,新的進程會來接手處理工做。服務器

下面就是平滑升級的基本步驟:ui

本文環境:Fedora系統 原始nginx版本1.8.1atom

升級目標:nginx平滑升級到1.9.12spa


步驟:unix

1)下載nginx最新的包。本文選擇的是nginx-1.9.12.tar.gzcode

2)按照本系列第一篇博客文章中介紹的,對nginx-1.9.12.tar.gz進行解壓,configure,make三個步驟。可是,這裏千萬別make install。由於咱們此次是升級,而不是安裝!!!htm

[neil@neilhost Downloads]$ cd ~/Documents/
[neil@neilhost Documents]$ ll
總用量 1800
drwxr-xr-x. 5 neil neil   4096 4月  22 2015 log
drwxr-xr-x. 4 neil neil   4096 2月  29 20:17 MyCraft
drwxr-xr-x. 9 neil neil   4096 3月   3 11:27 nginx-1.8.1
-rw-r-----. 1 neil neil 833473 3月   3 11:02 nginx-1.8.1.tar.gz
-rw-r-----. 1 neil neil 899183 3月   8 16:01 nginx-1.9.12.tar.gz
-rw-r--r--. 1 neil neil  81911 3月   3 13:40 nginxbushu.txt
drwxr-xr-x. 5 neil neil   4096 5月  23 2015 ProgramAPP
drwxr-xr-x. 3 neil neil   4096 6月   5 2015 workspace-sts-3.6.4.RELEASE
[neil@neilhost Documents]$ tar -zxvf nginx-1.9.12.tar.gz 
nginx-1.9.12/
nginx-1.9.12/auto/
nginx-1.9.12/conf/
nginx-1.9.12/contrib/
nginx-1.9.12/src/
nginx-1.9.12/configure
nginx-1.9.12/LICENSE
******此處略去XXX個字
nginx-1.9.12/auto/cc/msvc
nginx-1.9.12/auto/cc/name
nginx-1.9.12/auto/cc/owc
nginx-1.9.12/auto/cc/sunc
[neil@neilhost Documents]$ cd nginx-1.9.12/
[neil@neilhost nginx-1.9.12]$ ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[neil@neilhost nginx-1.9.12]$ ./configure
checking for OS
 + Linux 3.17.4-301.fc21.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
******此處略去XXX個字
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[neil@neilhost nginx-1.9.12]$ make
make -f objs/Makefile
make[1]: Entering directory '/home/neil/Documents/nginx-1.9.12'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
******此處略去XXX個字
objs/src/http/modules/ngx_http_upstream_hash_module.o \
objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \
objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
	-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/home/neil/Documents/nginx-1.9.12'
[neil@neilhost nginx-1.9.12]$ ll
總用量 688
drwxr-xr-x. 6 neil neil   4096 3月   8 16:02 auto
-rw-r--r--. 1 neil neil 259440 2月  24 22:53 CHANGES
-rw-r--r--. 1 neil neil 395257 2月  24 22:53 CHANGES.ru
drwxr-xr-x. 2 neil neil   4096 3月   8 16:02 conf
-rwxr-xr-x. 1 neil neil   2481 2月  24 22:53 configure
drwxr-xr-x. 4 neil neil   4096 3月   8 16:02 contrib
drwxr-xr-x. 2 neil neil   4096 3月   8 16:02 html
-rw-r--r--. 1 neil neil   1397 2月  24 22:53 LICENSE
-rw-rw-r--. 1 neil neil    332 3月   8 16:03 Makefile
drwxr-xr-x. 2 neil neil   4096 3月   8 16:02 man
drwxrwxr-x. 3 neil neil   4096 3月   8 16:03 objs
-rw-r--r--. 1 neil neil     49 2月  24 22:53 README
drwxr-xr-x. 9 neil neil   4096 3月   8 16:02 src
[neil@neilhost nginx-1.9.12]$

以後,按照升級的規範咱們須要對現有正在運行的nginx1.8.1的運行程序進行備份。

[neil@neilhost nginx-1.9.12]$ cd /usr/local/nginx/sbin/
[neil@neilhost sbin]$ ls
nginx
[neil@neilhost sbin]$ sudo cp nginx nginx.old
[sudo] password for neil: 
[neil@neilhost sbin]$ ls
nginx  nginx.old
[neil@neilhost sbin]$

這裏,咱們能夠先查看一下當前的狀態。當前nginx的版本,以及nginx運行的狀態:

[neil@neilhost sbin]$ pstree -p | grep nginx
           |-nginx(8562)---nginx(8563)
[neil@neilhost sbin]$ sudo ./nginx -V
nginx version: nginx/1.8.1
built by gcc 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) 
configure arguments:
[neil@neilhost sbin]$

以後,咱們就開始平滑升級。咱們將下載、解壓、configure、make好以後的nginx1.9.2的文件夾中的objs目錄下的nginx程序複製並替換當前正在運行的nginx的sbin目錄下的1.8.1版本的nginx。

[neil@neilhost objs]$ cd /usr/local/nginx/sbin/
[neil@neilhost sbin]$ ls
nginx  nginx.old
[neil@neilhost sbin]$ sudo ./nginx -V
nginx version: nginx/1.9.12
built by gcc 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) 
configure arguments:
[neil@neilhost sbin]$ pstree -p | grep nginx
           |-nginx(8562)---nginx(8563)
[neil@neilhost sbin]$

至此,咱們的nginx平滑升級就完成了。咱們發現,nginx的服務沒有中止,可是此時nginx的版本已經升級到了1.9.12的版本。同時nginx的主進程和服務進程也沒有所以改變ID。總體的過程是平滑的。而且,咱們還將老版本1.8.1的nginx進行備份,即nginx.old。


最後,若是你是個硬盤潔癖,就把那些下載的和make的代碼包刪除吧。

[neil@neilhost sbin]$ rm -f ~/Documents/nginx-1.9.12.tar.gz
[neil@neilhost sbin]$ rm -rf ~/Documents/nginx-1.9.12/
相關文章
相關標籤/搜索