Nginx-平滑升级

#一、为什么要对 nginx 平滑升级
随着 nginx 越来越流行,并且 nginx 的优势也越来越明显,nginx 的版本迭代也来时加速模式,1.9.0版本的nginx更新了许多新功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就需要平滑升级。

nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:

  • 在不停掉老进程的情况下,启动新进程。
  • 老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
  • 新进程接受新请求。
  • 老进程处理完所有请求,关闭所有连接后,停止。

这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。

#二、nginx 平滑升级描述
###多进程模式下的请求分配方式

nginx 默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接

###信号的接收和处理
nginx 主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。

###Nginx信号简介
#####主进程支持的信号

  • TERM, INT: 立刻退出
  • QUIT: 等待工作进程结束后再退出
  • KILL: 强制终止进程
  • HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
  • USR1: 重新打开日志文件
  • USR2: 启动新的主进程,实现热升级
  • WINCH: 逐步关闭工作进程

#####工作进程支持的信号

  • TERM, INT: 立刻退出
  • QUIT: 等待请求处理结束后再退出
  • USR1: 重新打开日志文件

#三、nginx 平滑升级实例
###1. 查看现有的 nginx 编译参数

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream

###2. 将新版本的源码包nginx-1.16.1.tar.gz,解压到/usr/local/
按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖

[root@localhost ~]# tar -xvzf nginx-1.16.1.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.16.1/

[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream

[root@localhost nginx-1.16.1]# make

###3. 备份原 nginx 二进制文件

[root@localhost nginx-1.16.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bk

###4. 复制新的nginx二进制文件,进入新的nginx源码包

[root@localhost nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/

[root@localhost nginx-1.16.1]# ls /usr/local/nginx/sbin/
nginx  nginx.bk

###5. 测试新版本的nginx是否正常

[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

###6. 给nginx发送平滑迁移信号(若不清楚pid路径,请查看nginx配置文件)
USR2:启动新的主进程,实现热升级

[root@localhost nginx-1.16.1]# kill -USR2 `cat /var/run/nginx.pid`

###7. 查看nginx pid,会出现一个nginx.pid.oldbin

[root@localhost nginx-1.16.1]# ll /var/run/nginx.pid*
-rw-r--r--. 1 root root 6 3月  12 13:26 /var/run/nginx.pid
-rw-r--r--. 1 root root 6 3月  12 13:26 /var/run/nginx.pid.oldbin

###8. 从容关闭旧的Nginx进程(WINCH:逐步关闭工作进程)

[root@localhost nginx-1.16.1]# kill -WINCH `cat /var/run/nginx.pid.oldbin`

###9. 此时不重载配置启动旧的工作进程
HUP:重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。

[root@localhost nginx-1.16.1]# kill -HUP `cat /var/run/nginx.pid.oldbin`

###10. 结束工作进程,完成此次升级
QUIT:等待请求处理结束后在退出

[root@localhost nginx-1.16.1]# kill -QUIT `cat /var/run/nginx.pid.oldbin`

###11. 验证Nginx是否升级成功

[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V

###12. 访问验证

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值