nginx 之热升级流程(平滑升级)

梦里梦外; 2023-07-02 07:28 130阅读 0赞

目录

1、安装nginx 1.14.2

2、编译新版本的nginx

按照第一步操作,下载新版本的nginx

3、热升级nginx


1、将旧nginx二进制文件换成新的nginx二进制 文件(注意备份)

2、向master进程发送USR2信号.

3、master进程修改pid文件名,加后缀.olidbin

4、master进程用新nginx文件启动新master进程

5、向老master进程发送QUIT信号,关闭老master进程

6、回滚: 向老master发送HUP,向新master发送QUIT

下面我们来演示一下 ,将nginx 从1.14.2 平滑升级到1.1.6.1版本

1、安装nginx 1.14.2

首先我们下载一个nginx-1.14.2 的版本

  1. wget https://nginx.org/download/nginx-1.14.2.tar.gz

解压之后,进行nginx 目录,运行如下命令,首先保证/soft/nginx 目录存在

  1. ./configure --prefix=/soft/nginx

打印如下:

  1. Configuration summary
  2. + using system PCRE library
  3. + OpenSSL library is not used
  4. + using system zlib library
  5. nginx path prefix: "/soft/nginx"
  6. nginx binary file: "/soft/nginx/sbin/nginx"
  7. nginx modules path: "/soft/nginx/modules"
  8. nginx configuration prefix: "/soft/nginx/conf"
  9. nginx configuration file: "/soft/nginx/conf/nginx.conf"
  10. nginx pid file: "/soft/nginx/logs/nginx.pid"
  11. nginx error log file: "/soft/nginx/logs/error.log"
  12. nginx http access log file: "/soft/nginx/logs/access.log"
  13. nginx http client request body temporary files: "client_body_temp"
  14. nginx http proxy temporary files: "proxy_temp"
  15. nginx http fastcgi temporary files: "fastcgi_temp"
  16. nginx http uwsgi temporary files: "uwsgi_temp"
  17. nginx http scgi temporary files: "scgi_temp"

完成之后,执行如下命令

  1. make
  2. make install

可以看到/soft/nginx 已经出现 编译后的nginx 文件了

  1. [root@zk02 nginx]# pwd
  2. /soft/nginx
  3. [root@zk02 nginx]# ls
  4. conf html logs sbin

在/etc/profile 文件配置如下

  1. #nginx
  2. export NGINX_HOME=/soft/nginx
  3. export PATH=$PATH:$NGINX_HOME/sbin

配置完成之后,source 一下.

  1. source /etc/profile

查看版本

  1. [root@zk02 ~]# nginx -v
  2. nginx version: nginx/1.14.2

以绝对启动nginx

  1. [root@zk02 sbin]# /soft/nginx/sbin/nginx

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bGVpX3Fx_size_16_color_FFFFFF_t_70

2、编译新版本的nginx

按照第一步操作,下载新版本的nginx

  1. wget https://nginx.org/download/nginx-1.16.1.tar.gz

编译该版本的源码

  1. ./configure --prefix=/soft/nginx

编译

  1. [root@zk02 nginx-1.16.1]# pwd
  2. /root/nginx_download/nginx-1.16.1
  3. [root@zk02 nginx-1.16.1]# make

在obj目下下可以看到编译好的nginx 文件

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bGVpX3Fx_size_16_color_FFFFFF_t_70 1

3、热升级nginx

我们看下老的nginx 进程 ,master 进程号为6965

  1. [root@zk02 sbin]# ps -ef | grep nginx
  2. root 6965 1 0 08:21 ? 00:00:00 nginx: master process /soft/nginx/sbin/nginx
  3. nobody 6966 6965 0 08:21 ? 00:00:00 nginx: worker process
  4. root 7017 6938 0 08:24 pts/1 00:00:00 grep --color=auto nginx

首先备份老的nginx 二进制文件,回滚的时候会用到

  1. [root@zk02 sbin]# cp nginx nginx.old
  2. [root@zk02 sbin]# ll
  3. total 7320
  4. -rwxr-xr-x 1 root root 3746440 Jan 28 07:28 nginx
  5. -rwxr-xr-x 1 root root 3746440 Jan 28 07:50 nginx.old

将新的nginx 文件覆盖旧的nginx 文件.

  1. [root@zk02 objs]# cp -f /root/nginx_download/nginx-1.16.1/objs/nginx /soft/nginx/sbin/
  2. cp: overwrite ?.soft/nginx/sbin/nginx?. y

给master 进程 执行USR2信号,会生成新的master和worker进程.

  1. [root@zk02 sbin]# kill -USR2 6965
  2. [root@zk02 sbin]# ps -ef | grep nginx
  3. root 6965 1 0 08:21 ? 00:00:00 nginx: master process /soft/nginx/sbin/nginx
  4. nobody 6966 6965 0 08:21 ? 00:00:00 nginx: worker process
  5. root 7021 6965 0 08:25 ? 00:00:00 nginx: master process /soft/nginx/sbin/nginx
  6. nobody 7022 7021 0 08:25 ? 00:00:00 nginx: worker process

可以看到pid文件名已更改

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bGVpX3Fx_size_16_color_FFFFFF_t_70 2

給老master进程发送QUIT 信号,优雅的退出

  1. [root@zk02 sbin]# kill -QUIT 6965
  2. [root@zk02 sbin]# ps -ef | grep nginx
  3. root 7021 1 0 08:25 ? 00:00:00 nginx: master process /soft/nginx/sbin/nginx
  4. nobody 7022 7021 0 08:25 ? 00:00:00 nginx: worker process
  5. root 7054 6938 0 08:34 pts/1 00:00:00 grep --color=auto nginx

至此,热升级完成.

注意: 升级的过程(kill -USR2 6965),可能报如下错误.

2020/01/28 08:03:06 [alert] 6819#0: execve() failed while executing new binary process “nginx” (2: No such file or directory)
以绝对路径执行nginx 文件即可.

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx平滑升级

    1、为什么要对 nginx 平滑升级 随着 `nginx` 越来越流行,并且 `nginx` 的优势也越来越明显,`nginx` 的版本迭代也来时加速模式,1.9.0版本

    相关 Nginx平滑升级

    一、环境准备 在业务不关闭情况下进行升级,并且不要版本差距太大,不然很多东西不支持容易崩溃 环境:centos7.3一台 版本:旧的1.8,新的1.10 部署目