Nginx笔记系列(3)——Nginx的平滑升级

什么是平滑升级?

对于一个正在运行中nginx服务器,如果我们需要对其进行升级到高版本的nginx。也许你会直接把nginx停止,然后完成升级操作,之后再次启动nginx。这种方式的缺点是显而易见的,一些正在处理的请求就会直接丢失了。(有点像狗血片里的XXX:“这日子没法儿过了!”刷东西。。。)强制升级,服务器上运行的进程会受到影响。因此,才需要在不停止nginx的情况下对nginx进行平滑升级。

平滑升级:不会停掉在运行着的进程,这些进程会继续处理请求,但不会再接收请求,这些老进程处理完正在处理的这些请求之后,会停止。(有点像武侠片里的XXX:“等我干完手上这几件事情之后就金盆洗手”)

在平滑升级中,新的进程会来接手处理工作。

下面就是平滑升级的基本步骤:

本文环境:Fedora系统 原始nginx版本1.8.1

升级目标:nginx平滑升级到1.9.12


步骤:

1)下载nginx最新的包。本文选择的是nginx-1.9.12.tar.gz

2)按照本系列第一篇博客文章中介绍的,对nginx-1.9.12.tar.gz进行解压,configure,make三个步骤。但是,这里千万别make install。因为我们这次是升级,而不是安装!!!

[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/






转载于:https://my.oschina.net/happyBKs/blog/632878

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值