nginx平滑升级
一、解释nginx的平滑升级
随着网站并发访问量越来越高,nginx web 服务器也越来越流行,nginx 版本换代越来越频繁,1.10.2版本的nginx更新了许多新功能,生产环境中版本升级必然的,但是线上业务不能停,此时nginx的升级就是运维的重要工作了。
二、nginx平滑升级原理
多进程模式下的请求分配方式
Nginx默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。
信号的接收和处理
Nginx主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。
Nginx信号简介
主进程支持的信号
TERM, INT: 立刻退出
QUIT: 等待工作进程结束后再退出
KILL: 强制终止进程
HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
USR1: 重新打开日志文件
USR2: 启动新的主进程,实现热升级
WINCH: 逐步关闭工作进程
工作进程支持的信号
TERM, INT: 立刻退出
QUIT: 等待请求处理结束后再退出
USR1: 重新打开日志文件
三、nginx平滑升级实战
[root@localhost ~]# rpm -q httpd
package httpd is not installed
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@localhost ~]# ll nginx-*
-rw-r–r-- 1 root root 910812 Nov 15 15:00 nginx-1.10.2.tar.gz
-rw-r–r-- 1 root root 804164 Dec 11 2014 nginx-1.6.2.tar.gz
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.2/
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# netstat -anpt |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4178/nginx
[root@localhost ~]# elinks --dump http://localhost
Welcome to nginx!
1.查看旧版nginx的编译参数
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.6.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module
2.编译新版本Nginx源码包,安装路径需与旧版一致,注意:不要执行make install
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.10.2/
[root@localhost nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make
3.备份二进制文件,用新版本的替换
[root@localhost nginx-1.10.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@localhost nginx-1.10.2]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@localhost nginx-1.10.2]# cp objs/nginx /usr/local/nginx/sbin/
4.确保配置文件无报错
[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5.发送USR2信号
向主进程(master)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@localhost ~]# ps aux | grep nginx | grep -v grep
root 4108 0.0 0.2 45028 1152 ? Ss 16:58 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4109 0.0 0.4 45456 2012 ? S 16:58 0:00 nginx: worker process
[root@localhost ~]# kill -USR2 4108
[root@localhost ~]# ps aux | grep nginx | grep -v grep
root 4108 0.0 0.2 45028 1316 ? Ss 16:58 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4109 0.0 0.4 45456 2012 ? S 16:58 0:00 nginx: worker process
root 6605 0.5 0.6 45196 3364 ? S 17:02 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6607 0.0 0.3 45624 1756 ? S 17:02 0:00 nginx: worker process
6.发送WINCH信号
向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@localhost ~]# kill -WINCH 4108
[root@localhost ~]# ps aux | grep nginx | grep -v grep
root 4108 0.0 0.2 45028 1320 ? Ss 16:58 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 6605 0.0 0.6 45196 3364 ? S 17:02 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6607 0.0 0.3 45624 1756 ? S 17:02 0:00 nginx: worker process
注意:回滚步骤,发送HUP信号
如果这时需要回退继续使用旧版本,可向旧的Nginx主进程发送HUP信号,它会重新启动工作进程, 仍使用旧版配置文件。然后可以将新版Nginx进程杀死(使用QUIT、TERM、或者KILL)
[root@localhost ~]# kill -HUP 4108
7.发送QUIT信号
升级完毕,可向旧的Nginx主进程(master)发送(QUIT、TERM、或者KILL)信号,使旧的主进程退出
[root@localhost ~]# kill -QUIT 4108
[root@localhost ~]# ps aux | grep nginx | grep -v grep
root 6605 0.0 0.6 45196 3364 ? S 17:02 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6607 0.0 0.4 45624 2056 ? S 17:02 0:00 nginx: worker process
8.验证nginx版本号,并访问测试
[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.10.2
- nginx平滑升级问题
- Nginx 的启动、停止、平滑重启、信号控制和平滑升级
- Nginx 的启动、停止、平滑重启、信号控制和平滑升级
- nginx多进程模型之热代码平滑升级
- nginx 平滑升级
- nginx平滑升级到0.8.53
- Nginx的平滑重启和平滑升级
- nginx平滑升级
- nginx的平滑升级
- nginx 平滑升级及ssl模块升级配置
- wdcp(nginx)平滑升级到Tengine 教程!无须重启
- Nginx平滑升级
- nginx 平滑升级
- Nginx的平滑重启和升级
- nginx的平滑升级,不间断服务
- Nginx平滑升级
- nginx 平滑重启与升级的实现方法
- 1分钟搞定Nginx版本的平滑升级与回滚的方法
- nginx启动、重启、重新加载配置文件和平滑升级
- nginx相关(平滑升级包括回退,错误页个性化,访问控制,日志推送)