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
[root@node5 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
[root@node5 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
[root@node5 objs]# cat /usr/local/nginx/logs/nginx.pid
43151

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

[root@node5 objs]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

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

[root@node5 objs]# cat /usr/local/nginx/logs/nginx.pid
45589

3)kill -WINCH 旧的主进程号
旧的主进程号收到WINCH信号后,将旧进程号管理的旧的工作进程优雅的关闭。即一段时间后旧的工作进程全部关闭,只有新的工作进程在处理请求连接。这时,依然可以恢复到旧的进程服务,因为旧的进程的监听socket还未停止。
处理完后,工作进程会自动关闭
[root@node5 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的升级完成。

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

查看
[root@node5 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  --新主进程号

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

  1. openresty开发系列2--nginx的简单安装,正向、反向代理及常用命令和信号控制介绍

    openresty开发系列2--nginx的简单安装,正向.反向代理及常用命令和信号控制介绍 一.nginx的安装下载及编译安装1.Nginx下载:nginx-1.13.0.tar.gz,下载到:/u ...

  2. openresty开发系列10--openresty的简单介绍及安装

    openresty开发系列10--openresty的简单介绍及安装 一.Nginx优点 十几年前,互联网没有这么火,软件外包开发,信息化建设,帮助企业做无纸化办公,收银系统,工厂erp,c/s架构偏 ...

  3. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  4. openresty开发系列38--通过Lua+Redis 实现动态封禁IP

    openresty开发系列38--通过Lua+Redis 实现动态封禁IP 一)需求背景为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝 ...

  5. openresty开发系列37--nginx-lua-redis实现访问频率控制

    openresty开发系列37--nginx-lua-redis实现访问频率控制 一)需求背景 在高并发场景下为了防止某个访问ip访问的频率过高,有时候会需要控制用户的访问频次在openresty中, ...

  6. openresty开发系列36--openresty执行流程之6日志模块处理阶段

    openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...

  7. openresty开发系列35--openresty执行流程之5内容content阶段

    openresty开发系列35--openresty执行流程之5内容content阶段 content 阶段 ---init阶段---重写赋值---重写rewrite---access content ...

  8. openresty开发系列34--openresty执行流程之4访问阶段

    openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...

  9. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令语法:i ...

随机推荐

  1. 关于bat文件的批处理

    Windows聚焦壁纸的保存目录 window+R>复制下面的路径>回车 %localappdata%\Packages\Microsoft.Windows.ContentDelivery ...

  2. samba 问题解决

    问题1:使用cifscreds 命令提示 You already have stashed credentials for bash-4.2$ cifscreds add 172.24.8.11 Yo ...

  3. ContextLoaderListener解析

    推荐:spring源码 每一个整合spring框架的项目中,总是不可避免地要在web.xml中加入这样一段配置. <!-- Spring配置文件开始 --> <context-par ...

  4. Python函数的基本使用

    在编程中,无论使用什么 编程语言,函数的使用都是非常广泛的,函数能够完成特定的功能,降低编程的难度和代码重用. 1.函数的定义: 函数是一段具有特定功能的.可重用的语句组,用函数名来表示并通过函数名进 ...

  5. fread/IO 模板

    namespace Fread { char cb[1<<15],*cs,*ct; #define getc (cs==ct&&(ct=(cs=cb)+fread(cb,1 ...

  6. 自定义mvc增删改查

    对t_mvc_book表的增删改查 导入jar包 BaseDao package com.hmc.util; import java.lang.reflect.Field; import java.s ...

  7. php.exe文件

    一.正常情况PHP文件的访问需要通过浏览器,访问Apache,才能运行一个php文件 php文件在Apache文件夹的站点根目录里 浏览器通过域名文件的形式访问 二.通过浏览器在不需要Apache服务 ...

  8. RookeyFrame 线上 添加Model

    线上添加好了模块,会在本地生成几个文件 类文件:Rookey.Frame.Web\Config\TempModel\Order_File.code DLL文件:Rookey.Frame.Web\bin ...

  9. codevs 1683 车厢重组

    1683 车厢重组  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver   题目描述 Description 在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥 ...

  10. Ancient Go(简单DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 AC代码: #include<iostream> #include<cstdi ...