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

nginx平滑升级

2015-10-26 23:53 736 查看
先来说下我今天要实验nginx平滑升级的环境,从nginx.1.8.0升级到nginx1.9.5

大概的流程:

Nginx的进程分为master主进程和work工作进程,master进程主要管理事件信号接受和分发,所有的请求处理都由work进程处理并返回结

果,Nginx的平滑重启或重载配置文件等升级,首先是向master发送重启或重载配置文件信号,然后master告诉所有的work进程不再接受新的

请求,然后master另起新的work进程,最后告诉旧的work进程可以光荣退出了

首先去nginx官网下载这两个tar.gz包

wget http://nginx.org//download/nginx-1.9.5.tar.gz wget http://nginx.org/download/nginx-1.8.0.tar.gz[/code] 
首先先安装好1.8.0

./configure --prefix=/usr/local/nginx --sbin-path=/sbin/nginx --conf-path=/etc/nginx.conf
make
make install
#启动nginx
/sbin/nginx
#查看nginx情况
[root@iZ283rmolcfZ ~]# netstat -anpl | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7940/nginx


如果在启动中报错:

[root@iZ283rmolcfZ nginx-1.8.0]# /sbin/nginx -t -c /etc/nginx.conf
nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/mime.types:1300
nginx: configuration file /etc/nginx.conf test failed

#解决方法:
[root@iZ283rmolcfZ nginx-1.8.0]# ll /etc/mime.types*
-rw-r--r--. 1 root root 43591 Sep 23  2011 /etc/mime.types
-rw-r--r--  1 root root  3957 Oct 16 18:06 /etc/mime.types.default
[root@iZ283rmolcfZ nginx-1.8.0]# cp /etc/mime.types /etc/mime.types.old
[root@iZ283rmolcfZ nginx-1.8.0]# cp /etc/mime.types.default /etc/mime.types


浏览器访问页面,响应头信息 Sever nginx/1.8.0

说明nginx1.8.0版本已安装,并且正在运行,接下来开始安装1.9.5版本

./configure --prefix=/usr/local/nginx --sbin-path=/sbin/nginx --conf-path=/etc/nginx.conf
make
make install

在make install时,会生成/sbin/nginx.old

(由于nginx进程已在内存中,所以硬盘文件的变动也不影响正在运行的nginx服务)

关键的步骤:

#更新 nginx.conf,不同版本的nginx,有可能nginx.conf是不一样的,所以我们要使用nginx1.9.5的conf文件。nginx.conf.defaule 就是刚刚在make install时候生成的
[root@iZ283rmolcfZ nginx-1.9.5]# ll /etc/nginx.conf*
-rw-r--r-- 1 root root 2656 Oct 16 18:06 /etc/nginx.conf
-rw-r--r-- 1 root root 2656 Oct 16 18:18 /etc/nginx.conf.default
[root@iZ283rmolcfZ nginx-1.9.5]# cp /etc/nginx.conf /etc/nginx.conf.old
[root@iZ283rmolcfZ nginx-1.9.5]# cp /etc/nginx.conf.default /etc/nginx.conf

#发送平滑升级信息,此时会生成/usr/local/nginx/logs/nginx.pig.oldbin
kill -s -SIGUSR2 `cat /usr/local/nginx/logs/nginx.pid`

#启动新版本nginx,此时在浏览器访问页面,有时候是1.8.0,有时候是1.9.5,但正在运行的进程pid不变
/sbin/nginx -s reload

#优雅退出旧版本nginx,再次刷新浏览器,页面的头部Sever nginx/1.9.5,
多次刷新后也是这样
kill -s SIGQUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

当Nginx关闭后,对于的pid也会被删除,只有启动后才会再生成一个新的pid

Nginx 的信号控制

TERM, INT 快速关闭

QUIT 从容关闭

HUP 平滑重启,重新加载配置文件

USR1 重新打开日志文件,在切割日志时用途较大

USR2 平滑升级可执行程序

WINCH 从容关闭工作进程

问题:

1.nginx1.8.0的nginx命令 是否可以给 1.9.0版本发送信息,如停止信号?

答:都是向同一个pid发送就可以
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: