Nginx平滑升级版本!

一、解释nginx的平滑升级

随着nginx越来越流行使用,并且nginx的优势也越来越明显,nginx的版本迭代也开始了加速模式,1.9.0版本的nginx更新了许多新功能,例如stream四层代理功能。伴随着nginx的广泛应用,版本升级必然是越来越快的,线上业务不能停,此时nginx的升级就是运维的重要工作了,下面就带大家一起来理解下nginx平滑升级。

二、nginx平滑升级原理

多进程模式下的请求分配方式

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

信号的接收和处理

Nginx主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。

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

三、nginx平滑升级实战

一:查看能不能通Welcome to nginx页面

[root@location ~]#rpm -q httpd

[root@location ~]#yum -y install gcc gcc-c++ make zlib-devel pcre-devel elinks 

[root@location ~]#ll nginx-*

[root@location ~]#useradd -M -s /sbin/nologin nginx

[root@location ~]#tail -l  /etc/passwd;tail -l /etc/group

[root@location ~]#elinks --dump http://location

                                                       Welcome to nginx

二:查看旧版本nginx的编译参数

[root@location ~]#nginx -V                       //本机为1.16.0版本nginx

nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

 

三:编译新版本Nginx源码包,安装路径必须与旧版本一致,且不能执行make install(本次用1.15.9的版本做实验)

3.1:为了实验的成功先杀死nginx进程

[root@localhost ~]# killall -9 nginx
[root@localhost ~]# nginx

浏览器查询是否可以正常访问

[root@localhost ~]#rz -E(此处上传新源码包,这里用1.15.9代替做实验)

[root@localhost ~]# ls
anaconda-ks.cfg         nginx-1.15.9.tar.gz    original-ks.cfg    模板    图片   下载     桌面
initial-setup-ks.cfg      nginx-1.16.0.tar.gz    公共   视频    文档     音乐

3.2:先解压1.15.9版本

[root@localhost ~]#tar xf nginx-1.15.9.tar.gz -C /usr/src

[root@localhost ~]#cd /usr/src/nginx-1.15.9/

[root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make

 

3.3:备份二进制文件用新版本代替

[root@localhost nginx-1.15.9]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old             //将旧版本的nginx改名并备份

[root@localhost nginx-1.15.9]# ls
auto    CHANGES.ru   configure    html    Makefile     objs       src
CHANGES    conf    contrib    LICENSE     man         README
[root@localhost nginx-1.15.9]# ls objs/
autoconf.err     nginx     ngx_auto_config.h        ngx_modules.c     src
Makefile          nginx.8      ngx_auto_headers.h      ngx_modules.o

[root@localhost nginx-1.15.9]# cp objs/nginx /usr/local/nginx/sbin
[root@localhost nginx-1.15.9]# ll /usr/local/nginx/sbin
总用量 7536
-rwxr-xr-x. 1 root root 3852128 9月 16 19:52 nginx                                  //新版本
-rwxr-xr-x. 1 root root 3858680 9月  16 17:09 nginx.old                           //旧版本

[root@localhost nginx-1.15.9]# nginx -t               //启动nginx,让新的配置文件加载旧的配置文件看兼容不兼容
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

四:进行信号处理(发送USR2信号)

主进程(master)发送USR2信号。Nginx会启动一个新版本的master进程和对应的工作进程

和旧版本一起处理请求

[root@localhost nginx-1.15.9]# cd
[root@localhost ~]# ps aux | grep nginx                                   //红色代表旧进程(看时间区分)
root 12467 0.0 0.6 156604 6764 pts/1 T 16:25 0:00 vim nginx.conf
root 68104 0.0 0.0 20560 660 ? Ss 19:14 0:00 nginx: master process ngin       //旧的主进程
nginx 68105 0.0 0.1 23032 1924 ? S 19:14 0:00 nginx: worker process             //旧的工作进程
nginx 68106 0.0 0.1 23032 1420 ? S 19:14 0:00 nginx: worker process              //旧的工作进程
root 71140 0.0 0.0 112724 992 pts/1 S+ 20:00 0:00 grep --color=auto nginx      

[root@localhost ~]# kill -USR2 68104                //杀掉旧进程主程序

 [root@localhost ~]# nginx -V

[root@localhost ~]# killall -9 nginx
[root@localhost ~]# nginx
[root@localhost ~]# ps aux | grep nginx

转载于:https://www.cnblogs.com/cxm123123form/p/11529988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值