nginx平滑升级与回滚

2024-09-01 06:20
文章标签 nginx 升级 回滚 平滑

本文主要是介绍nginx平滑升级与回滚,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

华子目录

  • 升级
    • 实验环境准备
    • 测试内容准备
    • 实验要求
    • 实验步骤
      • 1.解压包
      • 2.检测1.26版本的环境
      • 3.make编译
      • 4.备份之前的`nginx`启动脚本
      • 5.将1.26中的nginx启动脚本覆盖掉1.24中的
      • 6.`kill -USR2 旧主进程pid`
      • 7.`kill -WINCH 旧主进程pid`
    • 实验测试
  • 回滚
    • 1.`kill -HUP 旧主进程pid`
    • 2.`kill -WINCH 新主进程pid`
    • 3.备份`nginx`启动脚本
    • 4.`nginx.old`覆盖`nginx`
    • 5.`kill -9 新主进程pid`
    • 回滚测试

升级

实验环境准备

  • nginx版本:1.24
  • 需要升级到的版本:1.26
  • 需要的包:echo-nginx-module-0.63.tar.gznginx-1.24.0.tar.gznginx-1.26.2.tar.gz
[root@nginx ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg                nginx-1.24.0         nginx-1.26.2.tar.gz
模板  图片  下载  桌面  echo-nginx-module-0.63.tar.gz  nginx-1.24.0.tar.gz  vmset-rhel9-mountYum.sh

测试内容准备

[root@nginx ~]# cd /usr/local/nginx/html
[root@nginx html]# echo hello world > index.html[root@nginx html]# cat index.html
hello world
while true; do curl 172.25.254.100;sleep 1; done

在这里插入图片描述

实验要求

  • 在升级的过程中,原nginx提供的web服务不能中断
[root@nginx ~]# ps -ef | grep nginx
root       42795       1  0 09:31 ?        00:00:00 nginx: master process nginx
nginx      42796   42795  0 09:31 ?        00:00:00 nginx: worker process
root       42798   39792  0 09:31 pts/0    00:00:00 grep --color=auto nginx
[root@nginx ~]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0    #可以看到旧版本是1.24的
Date: Sat, 31 Aug 2024 13:32:21 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 31 Aug 2024 13:31:18 GMT
Connection: keep-alive
ETag: "66d31b26-267"
Accept-Ranges: bytes

实验步骤

1.解压包

[root@nginx ~]# tar -zxvf echo-nginx-module-0.63.tar.gz
[root@nginx ~]# tar -zxvf nginx-1.26.2.tar.gz

2.检测1.26版本的环境

[root@nginx ~]# cd nginx-1.26.2/
[root@nginx nginx-1.26.2]# ./configure --prefix=/usr/local/nginx \   #--prefix表示nginx安装的位置
> --user=nginx \   #指定nginx运行用户
> --group=nginx \   #指定nginx运行组
> --with-http_ssl_module \   #支持https://
> --with-http_v2_module \   #支持http版本2
> --with-http_realip_module \   #支持ip透传
> --with-http_stub_status_module \   #支持状态页面
> --with-http_gzip_static_module \   #支持压缩
> --with-pcre \      #支持正则
> --with-stream \    #支持tcp反向代理
> --with-stream_ssl_module   #支持tcp的ssl加密
> --add-module=/root/echo-nginx-module-0.63    #指定添加的模块路径(这个模块的作用:在nginx内部可以执行echo命令)

3.make编译

  • make编译,不要make install
[root@nginx nginx-1.26.2]# make

4.备份之前的nginx启动脚本

[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ls
nginx
[root@nginx sbin]# cp nginx nginx.old
[root@nginx sbin]# ls
nginx  nginx.old

5.将1.26中的nginx启动脚本覆盖掉1.24中的

#-f表示强制覆盖
[root@nginx sbin]# \cp -f /root/nginx-1.26.2/objs/nginx /usr/local/nginx/sbin/

6.kill -USR2 旧主进程pid

  • 使用kill -USR2启动一个新的只启动,不监听端口
[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832   928 ?        Ss   10:56   0:00 nginx: master process nginx
nginx      52798  0.0  0.1  13720  5348 ?        S    10:56   0:00 nginx: worker process
root       52800  0.0  0.0 221680  2252 pts/0    S+   10:57   0:00 grep --color=auto nginx[root@nginx sbin]# pidof nginx
52798 52797[root@nginx sbin]# kill -USR2 52797      #指定master的pid[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
nginx      52798  0.0  0.1  13720  5348 ?        S    10:56   0:00 nginx: worker process
root       52823  0.0  0.1   9864  5912 ?        S    10:58   0:00 nginx: master process nginx
nginx      52824  0.0  0.1  13752  5308 ?        S    10:58   0:00 nginx: worker process
root       52826  0.0  0.0 221680  2324 pts/0    S+   10:58   0:00 grep --color=auto nginx
  • 此时访问还在继续

在这里插入图片描述

7.kill -WINCH 旧主进程pid

  • 使用kill -WINCH把旧的主进程回收掉
[root@nginx sbin]# kill -WINCH 52797[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
root       52823  0.0  0.1   9864  5912 ?        S    10:58   0:00 nginx: master process nginx
nginx      52824  0.0  0.1  13752  5308 ?        S    10:58   0:00 nginx: worker process
root       52842  0.0  0.0 221680  2316 pts/0    S+   11:03   0:00 grep --color=auto nginx
  • 此时访问还在继续

在这里插入图片描述

实验测试

[root@nginx sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.26.2   #可以看到版本升级成功
Date: Sat, 31 Aug 2024 15:04:16 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Sat, 31 Aug 2024 14:43:41 GMT
Connection: keep-alive
ETag: "66d32c1d-c"
Accept-Ranges: bytes

在这里插入图片描述

  • 发现web服务也没有中断过

回滚

1.kill -HUP 旧主进程pid

[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
root       52823  0.0  0.1   9864  5912 ?        S    10:58   0:00 nginx: master process nginx
nginx      52824  0.0  0.1  13752  5308 ?        S    10:58   0:00 nginx: worker process
root       52848  0.0  0.0 221680  2344 pts/0    S+   11:06   0:00 grep --color=auto nginx[root@nginx sbin]# kill -HUP 52797[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
root       52823  0.0  0.1   9864  5912 ?        S    10:58   0:00 nginx: master process nginx
nginx      52824  0.0  0.1  13752  5308 ?        S    10:58   0:00 nginx: worker process
nginx      52849  0.0  0.1  13720  5348 ?        S    11:08   0:00 nginx: worker process
root       52851  0.0  0.0 221680  2356 pts/0    S+   11:08   0:00 grep --color=auto nginx

2.kill -WINCH 新主进程pid

  • 新的主进程回收了
[root@nginx sbin]# kill -WINCH 52823[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
root       52823  0.0  0.1   9864  6416 ?        S    10:58   0:00 nginx: master process nginx
nginx      52849  0.0  0.1  13720  5348 ?        S    11:08   0:00 nginx: worker process
root       52858  0.0  0.0 221680  2352 pts/0    S+   11:10   0:00 grep --color=auto nginx

3.备份nginx启动脚本

[root@nginx sbin]# ls
nginx  nginx.old
[root@nginx sbin]# cp nginx nginx.new
[root@nginx sbin]# ls
nginx  nginx.new  nginx.old

4.nginx.old覆盖nginx

[root@nginx sbin]# \cp -f nginx.old nginx
[root@nginx sbin]# ls
nginx  nginx.new  nginx.old

5.kill -9 新主进程pid

[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
root       52823  0.0  0.1   9864  6416 ?        S    10:58   0:00 nginx: master process nginx
nginx      52849  0.0  0.1  13720  5348 ?        S    11:08   0:00 nginx: worker process
root       52880  0.0  0.0 221680  2240 pts/0    S+   11:15   0:00 grep --color=auto nginx[root@nginx sbin]# kill -9 52823[root@nginx sbin]# ps -aux | grep nginx
root       52797  0.0  0.0   9832  2396 ?        Ss   10:56   0:00 nginx: master process nginx
nginx      52849  0.0  0.1  13720  5348 ?        S    11:08   0:00 nginx: worker process
root       52883  0.0  0.0 221680  2352 pts/0    S+   11:15   0:00 grep --color=auto nginx

回滚测试

[root@nginx sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0   #发现已经回滚到旧版本
Date: Sat, 31 Aug 2024 15:12:01 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Sat, 31 Aug 2024 14:43:41 GMT
Connection: keep-alive
ETag: "66d32c1d-c"
Accept-Ranges: bytes

在这里插入图片描述

  • 发现web服务也没有中断过

这篇关于nginx平滑升级与回滚的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1126183

相关文章

关于Nginx跨域问题及解决方案(CORS)

《关于Nginx跨域问题及解决方案(CORS)》文章主要介绍了跨域资源共享(CORS)机制及其在现代Web开发中的重要性,通过Nginx,可以简单地解决跨域问题,适合新手学习和应用,文章详细讲解了CO... 目录一、概述二、什么是 CORS?三、常见的跨域场景四、Nginx 如何解决 CORS 问题?五、基

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

nginx部署https网站的实现步骤(亲测)

《nginx部署https网站的实现步骤(亲测)》本文详细介绍了使用Nginx在保持与http服务兼容的情况下部署HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录步骤 1:安装 Nginx步骤 2:获取 SSL 证书步骤 3:手动配置 Nginx步骤 4:测

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

nginx-rtmp-module构建流媒体直播服务器实战指南

《nginx-rtmp-module构建流媒体直播服务器实战指南》本文主要介绍了nginx-rtmp-module构建流媒体直播服务器实战指南,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. RTMP协议介绍与应用RTMP协议的原理RTMP协议的应用RTMP与现代流媒体技术的关系2

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

怎么关闭Ubuntu无人值守升级? Ubuntu禁止自动更新的技巧

《怎么关闭Ubuntu无人值守升级?Ubuntu禁止自动更新的技巧》UbuntuLinux系统禁止自动更新的时候,提示“无人值守升级在关机期间,请不要关闭计算机进程”,该怎么解决这个问题?详细请看... 本教程教你如何处理无人值守的升级,即 Ubuntu linux 的自动系统更新。来源:https://