1分钟搞定Nginx版本的平滑升级与回滚的方法

 更新时间:2019年10月13日 08:50:58   作者:民工哥技术之路  
这篇文章主要介绍了1分钟搞定Nginx版本的平滑升级与回滚的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

今天,我们来聊一聊,在企业实际生产环境中经常遇到的一个情况,升级Nginx到新的版本和如何回滚至旧版本。

1、环境介绍

今天准备的两个nginx版本如下:

[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# ll
total 1952
-rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz
-rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz

2、编译安装新旧版本

编译安装nginx-1.12.2

[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz 
[root@nginx nginx]# cd nginx-1.12.2
[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:01 conf
drwxr-xr-x 2 root root 40 Mar 1 09:01 html
drwxr-xr-x 2 root root  6 Mar 1 09:01 logs
drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin

编译安装nginx-1.14.2

[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz 
[root@nginx nginx]# cd nginx-1.14.2
[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# make && make install
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:03 conf
drwxr-xr-x 2 root root 40 Mar 1 09:03 html
drwxr-xr-x 2 root root  6 Mar 1 09:03 logs
drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin

到这里,两个版本的nginx软件已经部署完成。

3、启动旧版本nginx

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx
[root@nginx ~]# ps -ef|grep nginx
root    6324   1 0 09:06 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6325  6324 0 09:06 ?    00:00:00 nginx: worker process
root    6327  1244 0 09:06 pts/0  00:00:00 grep --color=auto nginx
[root@nginx ~]# lsof -i :80
COMMAND PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
nginx  6324  root  6u IPv4 26324   0t0 TCP *:http (LISTEN)
nginx  6325 nobody  6u IPv4 26324   0t0 TCP *:http (LISTEN)

4、升级到新版本

版本升级其实就是针对二进制文件的升级,过程如下:

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.12.2
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.12.2
#首先备份原来的旧版本nginx二进制文件
[root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./
#拷贝新版本的二进制文件到当前目录

接下来进行平滑升级操作

[root@nginx ~]# ps -ef|grep nginx
root    6324   1 0 09:06 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6325  6324 0 09:06 ?    00:00:00 nginx: worker process
root    6338  1244 0 09:11 pts/0  00:00:00 grep --color=auto nginx
[root@nginx ~]# kill -USR2 6324
[root@nginx ~]# ps -ef|grep nginx
root    6324   1 0 09:06 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6325  6324 0 09:06 ?    00:00:00 nginx: worker process
root    6340  6324 0 09:12 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6341  6340 0 09:12 ?    00:00:00 nginx: worker process
root    6343  1244 0 09:12 pts/0  00:00:00 grep --color=auto nginx

这时新的master进程已经正常开启,但老的work进程也存在,所以我们使用下面的命令,将老的work进程发出平滑停止的信号,如下:

[root@nginx ~]# kill -WINCH 6324
[root@nginx ~]# ps -ef|grep nginx
root    6324   1 0 09:06 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root    6340  6324 0 09:12 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6341  6340 0 09:12 ?    00:00:00 nginx: worker process
root    6346  1244 0 09:14 pts/0  00:00:00 grep --color=auto nginx

此时,老的work进程已经停止,接下来我们测试是否能正常访问:

可以正常访问,其实这一平滑升级的动作,对访问用户来说是完全感知不到,所以nginx热部署就已经完成了。

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.14.2

查看版本也是最新的版本,升级完成。

注:如果在版本升级完成后,没有任何问题,需要关闭老的master进程的话,可以使用下面的命令:

kill -QUIT old_master_PID

5、版本回滚

对于升级来说,最难的不是升级,而是回滚,因为在实际生产环境回滚的机率是存在,比如:新版本由于某些未知bug导致与现有应用不兼容、或出现运行不稳定的情况等等。

所以,对运维工程师来说,故障回滚是重点。

在上面的结果中,我们也能看到老的master进程是一直存在,在没有手工关闭前,它是不会自已关闭的,这种设计是有好处的,好处就是为了升级新版本后,如果出现问题能及时快速的回滚到上一个稳定版本。

[root@nginx ~]# ps -ef|grep nginx
root    6324   1 0 09:06 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root    6340  6324 0 09:12 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6341  6340 0 09:12 ?    00:00:00 nginx: worker process
root    6350  1244 0 09:23 pts/0  00:00:00 grep --color=auto nginx
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.14.2
[root@nginx sbin]# mv nginx-1.12.2 nginx
[root@nginx sbin]# kill -USR1 6324
[root@nginx sbin]# ps -ef|grep nginx
root    6324   1 0 09:06 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root    6340  6324 0 09:12 ?    00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody   6341  6340 0 09:12 ?    00:00:00 nginx: worker process
root    6355  1244 0 09:24 pts/0  00:00:00 grep --color=auto nginx
[root@nginx sbin]# ./nginx -v
nginx version: nginx/1.12.2

从上面的结果发现,已经平滑的回滚的上一个版本,接下来测试是否能正常访问:

一样可以正常访问,所以,这个回滚的操作对用户来说也是不可感知的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Nginx设置wordpress伪静态的方法示例

    Nginx设置wordpress伪静态的方法示例

    伪静态是相对真实静态来讲的,通常我们为了增强搜索引擎的友好面,这篇文章主要介绍了Nginx设置wordpress伪静态的方法示例,非常具有实用价值,需要的朋友可以参考下
    2018-09-09
  • Nginx访问日志及错误日志参数说明

    Nginx访问日志及错误日志参数说明

    这篇文章主要介绍了Nginx访问日志及错误日志参数说明,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • Nginx代理缓冲proxy_buffering配置方式

    Nginx代理缓冲proxy_buffering配置方式

    这篇文章主要介绍了Nginx代理缓冲proxy_buffering配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Nginx 简单的负载均衡配置示例

    Nginx 简单的负载均衡配置示例

    Nginx 简单的负载均衡配置示例,需要的朋友可以参考下。
    2009-10-10
  • Nginx Lua 缓存配置的实现步骤

    Nginx Lua 缓存配置的实现步骤

    在Web应用缓存层次中,Nginx Lua缓存因其高效的协程机制,与Nginx的集成,以及OpenResty的扩展性,成为了一种高性能的缓存解决方案,本文就来详细介绍,感兴趣的可以了解一下
    2022-05-05
  • Nginx如何配置前端后端服务

    Nginx如何配置前端后端服务

    本文介绍了如何配置Nginx作为前端静态资源服务器和反向代理服务器以连接后端应用,配置步骤包括设置Nginx的基本配置语法和结构,配置Nginx作为前端静态资源服务器和反向代理服务器,测试和优化Nginx配置,以及提供了一个示例配置
    2025-01-01
  • 浅谈一下Nginx性能优化

    浅谈一下Nginx性能优化

    这篇文章主要介绍了Nginx性能优化,Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务,需要的朋友可以参考下
    2023-04-04
  • 在Nginx中增加对OAuth协议的支持的教程

    在Nginx中增加对OAuth协议的支持的教程

    这篇文章主要介绍了在Nginx中增加对OAuth协议的支持的教程,OAuth协议如今广泛用于社交网络的API中,需要的朋友可以参考下
    2015-06-06
  • Nginx和Apache几种防盗链配置方法实例

    Nginx和Apache几种防盗链配置方法实例

    这篇文章主要介绍了Nginx和Apache几种防盗链配置方法实例,本文使用判断来路的方法实现防盗链,分别给出Nginx和Apache配置实例,需要的朋友可以参考下
    2015-02-02
  • 一篇文章读懂nginx的gzip_static模块

    一篇文章读懂nginx的gzip_static模块

    gzip是针对于请求实时进行压缩,cpu开销大,gzip_static 完全可以在编译后使用压缩工具搞出来,下面这篇文章主要给大家介绍了如何通过一篇文章读懂nginx的gzip_static模块,需要的朋友可以参考下
    2022-05-05

最新评论