openresty开发系列3--nginx的平滑升级

nginx服务器从低版本升级为高版本,如果强行停止服务,会影响正在运行的进程。

平滑升级不会停掉正在运行中的进程,这些进程会继续处理请求。但不会接受新请求,这些老的进程在处理完请求之后会停止。此平滑升级过程中,新开的进程会被处理。

一)平滑升级
进入nginx可执行程序的目录
     #  cd /usr/local/nginx/sbin/
     # sbin/nginx -v
       nginx version: nginx/1.13.0    #查看nginx版本

1)下载高版本nginx
wget http://nginx.org/download/nginx-1.13.2.tar.gz

执行指令生成版本的Nginx二进制程序
#  ./configure
# make    #不能执行 make install
# cd objs
此目录下 有高版本的nginx
备份低版本的nginx
cd /usr/local/nginx/sbin/
cp nginx nginx.old
执行强制覆盖,将低版本的nginx替换为刚编译好的高版本的nginx
[[email protected] objs]# cp -rfp /usr/local/src/nginx-1.13.2/objs/nginx /usr/local/nginx/sbin/
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

测试一下新复制过来文件生效情况:
# /usr/local/nginx/sbin/nginx -t
[[email protected] objs]# ps -ef|grep nginx
root      43151      1  0 19:40 ?        00:00:00 nginx: master process /usr/local//nginx/sbin/nginx
nobody    43152  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43153  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43154  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43155  43151  0 19:40 ?        00:00:00 nginx: worker process
root      45585  43080  0 19:46 pts/1    00:00:00 grep --color=auto nginx
[[email protected] objs]# cat /usr/local/nginx/logs/nginx.pid
43151

2)执行信号平滑升级
# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`  更新配置文件

[[email protected] objs]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

给nginx发送USR2信号后,nginx会将logs/nginx.pid文件重命名为nginx.pid.oldbin,然后用新的可执行文件启动一个新的nginx主进程和对应的工作进程,并新建一个新的nginx.pid保存新的主进程号

[[email protected] objs]# cat /usr/local/nginx/logs/nginx.pid
45589

3)kill -WINCH 旧的主进程号
旧的主进程号收到WINCH信号后,将旧进程号管理的旧的工作进程优雅的关闭。即一段时间后旧的工作进程全部关闭,只有新的工作进程在处理请求连接。这时,依然可以恢复到旧的进程服务,因为旧的进程的监听socket还未停止。
处理完后,工作进程会自动关闭
[[email protected] objs]# ps -ef|grep nginx
root      43151      1  0 19:40 ?        00:00:00 nginx: master process /usr/local//nginx/sbin/nginx
nobody    43152  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43153  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43154  43151  0 19:40 ?        00:00:00 nginx: worker process
nobody    43155  43151  0 19:40 ?        00:00:00 nginx: worker process
root      45589  43151  0 19:46 ?        00:00:00 nginx: master process /usr/local//nginx/sbin/nginx
nobody    45590  45589  0 19:46 ?        00:00:00 nginx: worker process
nobody    45591  45589  0 19:46 ?        00:00:00 nginx: worker process
nobody    45592  45589  0 19:46 ?        00:00:00 nginx: worker process
nobody    45593  45589  0 19:46 ?        00:00:00 nginx: worker process
root      45595  43080  0 19:46 pts/1    00:00:00 grep --color=auto nginx

4)# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` 优雅的关闭
给旧的主进程发送QUIT信号后,旧的主进程退出,并移除logs/nginx.pid.oldbin文件,nginx的升级完成。

升级完成了,最后在看一下升级后的版本

查看
[[email protected] objs]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.13.2

已经平滑升级成功

二)中途停止升级,回滚到旧的nginx

在步骤(3)时,如果想回到旧的nginx不再升级

(1)给旧的主进程号发送HUP命令,此时nginx不重新读取配置文件的情况下重新启动旧主进程的工作进程。
kill -HUP 43151 --旧主进程号
重启工作进程

(2)优雅的关闭新的主进程
kill -QUIT 45589  --新主进程号

原文地址:https://www.cnblogs.com/reblue520/p/11428939.html

时间: 2024-08-29 15:38:19

openresty开发系列3--nginx的平滑升级的相关文章

源码安装nginx以及平滑升级

作者:尹正杰 这个博客不方便上传软件包,我给大家把软件包放到百度云了: 链接:http://pan.baidu.com/s/1eS3bn4u 密码:04a1 欢迎加入:高级运维工程师之路 598432640 操作平台: 1.创建ngxin用户,(用于管理nginx服务,您也可以随意指定的哟~) useradd -s /sbin/nologin nginx 2.安装基础环境 yum -y install gcc pcre-devel openssl-devel zlib-devel 3.安装ngi

nginx的平滑升级方法:

最简单的nginx的平滑升级方法: 1 找到nginx的执行文件的路径 # ps auxf|grep nginx  记下nginx的master进程 pid(我这里是2752 ) 2 查看当前nginx的版本及编译参数: # nginx -V nginx version: nginx/1.8.0 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS S

openresty开发系列16--lua中的控制结构if-else/repeat/for/while

openresty开发系列16--lua中的控制结构if-else/repeat/for/while 一)条件 - 控制结构 if-else if-else 是我们熟知的一种控制结构.Lua 跟其他语言一样,提供了 if-else 的控制结构. 1)单个 if 分支 型 if 条件 then --body end 条件为真 ,执行if中的body ----------------------- x = 10 if x > 0 then print("分支一") end ----

nginx不间断服务平滑升级

(1)备份旧的nginx和配置文件 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/bak_nginx #备份旧版程序 cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/bak_nginx.conf #备份配置文件 (2)编译安装新版本  ./configure --user=www --group=www --prefix=/usr/local/nginx --with-

openresty开发系列24--openresty中lua的引入及使用

openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua   --->字符串编写方式  2) xxx_by_lua_block ---->代码块方式  3) xxx_by_lua_file  ---->直接引用一个lua脚本文件 我们案例中使用内容处理阶段,用content_by_lua演示 -----------------编辑nginx.conf----------------------- 第一种:content_by_lua l

Nginx的平滑升级

环境说明 当前服务器中正在运行Nginx服务,现想将当前运行的Nginx服务的版本经行升级,(从1.6升级到1.8,版本的跨度不要太大,容易造成服务的崩溃),且在服务不停止的前提下经行升级. 1. 在不停掉老进程的情况下,启动新进程. 2. 老进程负责处理仍然没有处理完的请求,但不再接受处理请求. 3. 新进程接受新请求. 4. 老进程处理完所有请求,关闭所有连接后,停止. 实现步骤 1. 编译安装nginx-1.6 yum -y install pcre-devel zlib-devel #安

openresty开发系列1--网关API架构及选型

微服务架构在项目中的应用越来越多,我们知道在微服务架构风格中,一个大应用被拆分成为了多个小的服务系统提供出来,这些小的系统他们可以自成体系,也就是说这些小系统可以拥有自己的数据库,框架甚至语言等,这些小系统通常以提供 Rest Api 风格的接口来被 H5, Android, IOS 以及第三方应用程序调用.但是在UI上进行展示的时候,我们通常需要在一个界面上展示很多数据,这些数据可能来自于不同的微服务中,举个例子.    在一个电商系统中,查看一个商品详情页,这个商品详情页包含商品的标题,价格

openresty开发系列25--openresty中使用json模块

web开发过程中,经常用的数据结构为json,openresty中封装了json模块,我们看如何使用 一)如何引入cjson模块,需要使用requirelocal json = require("cjson") json.encode 将表格数据编码为 JSON 字符串格式:jsonString = json.encode(表格对象)用法示例: table 包含哈希键值对 和 数组键值对 -------------------test.lua-------------- 1)table

openresty开发系列20--lua的时间操作

在 Lua 中,函数 time.date 和 difftime 提供了所有的日期和时间功能.在 OpenResty 的世界里,不推荐使用这里的标准时间函数,因为这些函数通常会引发不止一个昂贵的系统调用,同时无法为 LuaJIT JIT 编译,对性能造成较大影响.推荐使用 ngx_lua 模块提供的带缓存的时间接口,如 ngx.today, ngx.time, ngx.utctime, ngx.localtime, ngx.now, ngx.http_time,以及 ngx.cookie_time