您的位置:首页 > 运维架构 > Nginx

Nginx的“安静”升级

2012-05-22 09:12 239 查看
前言:

众所周知,开源软件的升级是快的没得商量。Nginx也不例外,在网上铺天盖地的0.7、0.8版本的使用、升级、优化的方法的时候,Nginx官方5.15号已经更新到了1.3.0(开发版)。对于线上的Nginx服务器,Nginx提供了很好的升级解决方案,可以在不中断服务的情况下,使用新版本、添加/删除服务器模块等,这也是我今天要写的主题《Nginx的“安静”升级》。

------------------------------------------------------------------------------------

步骤如下:

1)Nginx的旧版本的安装。(线上的服务器应该先已经安装Nginx的比较旧的版本)

详细安装步骤可参考我的另一篇博文:《Nginx服务器的安装配置/article/4355839.html

2)查看一下当前的Nginx版本。

#  nginx -v
nginx version: nginx/0.7.69

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

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

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

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

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

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

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

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

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

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

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

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

9)查看当前版本号。

# nginx -v
nginx version: nginx/1.3.0






到这里Nginx的“安静”升级就成功了。

------------------------------------------------------------------

附上Nginx支持的几种控制信号:

*  TERM,INT  快速关闭
*  QUIT   从容关闭
*  HUP    平滑重启,重新加载配置文件
*  USR1   重新打开日志文件,在切割日志时用途较大
*  USR2   平滑升级可执行程序
*  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
本文出自 “迷你兔” 博客,请务必保留此出处http://minitoo.blog.51cto.com/4201040/872215
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: