前言:

众所周知,开源软件的升级是快的没得商量。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版本。

  1. #  nginx -v  
  2. nginx version: nginx/0.7.69 

3)使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx,可以将新版本编译安装到旧版本的Nginx安装路径中,替换之前,您最好备份一下旧的可执行文件。

  1. # cp /usr/local/sbin/nginx ~/  
  2. # tar zxf nginx-1.3.0.tar.gz  
  3. # cd nginx-1.3.0  
  4. # ./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  
  5. # make && make install 

4)查看一下当前的主进程号是多少,然后执行命令:kill -USR2 旧的版本的Nginx主进程号   。

  1. # ps -ef | grep nginx  
  2. root     21421     1  0 20:37 ?        00:00:00 nginx: master process nginx  
  3. nobody   24021 21421  0 20:49 ?        00:00:00 nginx: worker process  
  4. root     28765 24076  0 22:04 pts/1    00:00:00 grep nginx  
  5. # kill -USR2 21421 

5)旧的版本Nginx的主进程将重命名它的.pid文件为.oldbin(例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新的版本的Nginx可执行程序,依次启动新的主进程和新的工作进程。

  1. # /usr/local/sbin/nginx  
  2. # ps -ef | grep nginx  
  3. root     21421     1  0 20:37 ?        00:00:00 nginx: master process nginx  
  4. nobody   24021 21421  0 20:49 ?        00:00:00 nginx: worker process  
  5. root     28768 21421  0 22:04 ?        00:00:00 nginx: master process nginx  
  6. nobody   28769 28768  0 22:04 ?        00:00:00 nginx: worker process  
  7. root     28786 24076  0 22:11 pts/1    00:00:00 grep nginx 

6)此时,新、旧版本的Nginx实例会同时运行,共同处理输入的请求。要逐步停止旧版本的Nginx实例,您必须发送WINCH信号给旧的主进程,然后,它的工作进程就将开始从容关闭。

  1. # kill -WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin` 

7)一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了:

  1. # ps -ef | grep nginx  
  2. root     21421     1  0 20:37 ?        00:00:00 nginx: master process nginx  
  3. root     28768 21421  0 22:04 ?        00:00:00 nginx: master process nginx  
  4. nobody   28769 28768  0 22:04 ?        00:00:00 nginx: worker process  
  5. root     28799 24076  0 22:15 pts/1    00:00:00 grep nginx 

8)发送QUIT信号给旧的主进程,使其退出而只留下新的Nginx服务器运行。

  1. # kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`  
  2. # ps -ef | grep nginx  
  3. root     28768     1  0 22:04 ?        00:00:00 nginx: master process nginx  
  4. nobody   28769 28768  0 22:04 ?        00:00:00 nginx: worker process  
  5. root     28808 24076  0 22:20 pts/1    00:00:00 grep nginx 

9)查看当前版本号。

  1. # nginx -v  
  2. nginx version: nginx/1.3.0  


Nginx的“安静”升级_服务器

到这里Nginx的“安静”升级就成功了。
------------------------------------------------------------------
附上Nginx支持的几种控制信号:

  1. *  TERM,INT  快速关闭  
  2. *  QUIT   从容关闭  
  3. *  HUP    平滑重启,重新加载配置文件  
  4. *  USR1   重新打开日志文件,在切割日志时用途较大  
  5. *  USR2   平滑升级可执行程序  
  6. *  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