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@iZwz994oywnbrdsnn4hrkqZ /]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-stream --with-http_gzip_static_module

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

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.14.2.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.16.0.tar.gz -C /usr/src

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

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

 

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

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

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

[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.16.0]# cp objs/nginx /usr/local/nginx/sbin
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? yes
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.16.0]# ll /usr/local/nginx/sbin
total 3772

[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.16.0]# cd /usr/src/nginx-1.14.2/

[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.14.2]# mv objs/nginx nginx.old
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.14.2]# cp nginx.old /usr/local/nginx/sbin/
[root@iZwz994oywnbrdsnn4hrkqZ nginx-1.14.2]# ll /usr/local/nginx/sbin/
total 8044 
-rwxr-xr-x 1 root root 3858728 Feb 6 15:42 nginx                    #新版本
-rwxr-xr-x 1 root root 4373160 Feb 6 15:44 nginx.old               #旧版本

[root@localhost nginx-1.16.0]# 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.16.0]# cd

[root@iZwz994oywnbrdsnn4hrkqZ ~]# ps -aux | grep nginx
root 19604 0.0 0.1 20560 1232 ? Ss 15:45 0:00 nginx: master process nginx     #旧进程
nginx 19607 0.0 0.5 24812 5124 ? S 15:45 0:00 nginx: worker process              #旧进程
root 19609 0.0 0.0 112708 988 pts/0 R+ 15:45 0:00 grep --color=auto nginx

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@iZwz994oywnbrdsnn4hrkqZ ~]# nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

 

posted @ 2019-09-16 21:03  Room、C  阅读(6982)  评论(3编辑  收藏  举报