前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx 平滑升级与回滚

Nginx 平滑升级与回滚

作者头像
剧终
发布2020-10-16 10:41:18
6420
发布2020-10-16 10:41:18
举报
文章被收录于专栏:Linux学习日志Linux学习日志

环境

首先准备两个版本不一样Nginx

代码语言:javascript
复制
https://nginx.org/download/nginx-1.18.0.tar.gz
https://nginx.org/download/nginx-1.14.2.tar.gz

下载Nginx

代码语言:javascript
复制
[root@localhost down]# https://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost down]# https://nginx.org/download/nginx-1.14.2.tar.gz
[root@localhost down]# ll 
总用量 2008
-rw-r--r--. 1 root root 1015384 12月  4 2018 nginx-1.14.2.tar.gz
-rw-r--r--. 1 root root 1039530 4月  21 22:33 nginx-1.18.0.tar.gz

安装依赖包

代码语言:javascript
复制
yum install -y wget gcc gcc-c++ make pcre pcre-deve zilib zlib-devel openssl-devel

编译安装旧版本Nginx

代码语言:javascript
复制
tar zxf nginx-1.14.2.tar.gz 
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install

编译新版本Nginx

代码语言:javascript
复制
tar zxf nginx-1.18.0.tar.gz 
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make  #千万不要输入make install命令,否则会覆盖

Nginx 平滑升级

启动旧版本Nginx

代码语言:javascript
复制
/usr/local/nginx/sbin/nginx 

备份旧版本Nginx二进制文件

代码语言:javascript
复制
[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# cp nginx{,.bak}
[root@localhost sbin]# ls
nginx  nginx.bak
#复制新版本二进制文件到当前目录
[root@localhost sbin]# cp /down/nginx-1.18.0/objs/nginx ./nginx

发送USR2信号

代码语言:javascript
复制
[root@localhost sbin]# ps -ef|grep nginx
root     21951     1  0 00:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21952 21951  0 00:30 ?        00:00:00 nginx: worker process
root     21954  1436  0 00:30 pts/1    00:00:00 grep --color=auto nginx

#向主进程master发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@localhost sbin]# kill -USR2 21951

[root@localhost sbin]# ps -ef|grep nginx
root     21951     1  0 00:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21952 21951  0 00:30 ?        00:00:00 nginx: worker process
root     21961 21951  0 00:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21962 21961  0 00:31 ?        00:00:00 nginx: worker process
root     21964  1436  0 00:31 pts/1    00:00:00 grep --color=auto nginx

发送WINCH信号

代码语言:javascript
复制
#向旧的Nginx主进程master发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@localhost sbin]# kill -WINCH 21951
[root@localhost sbin]# ps -ef|grep nginx
root     21951     1  0 00:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root     21961 21951  0 00:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21962 21961  0 00:31 ?        00:00:00 nginx: worker process
root     21993  1436  0 00:41 pts/1    00:00:00 grep --color=auto nginx
代码语言:javascript
复制
[root@localhost sbin]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 10 Oct 2020 16:43:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 10 Oct 2020 16:28:21 GMT
Connection: keep-alive
ETag: "5f81e125-264"
Accept-Ranges: bytes

版本回滚

代码语言:javascript
复制
#如果这时需要回退继续使用旧版本,可向旧的Nginx主进程发送HUP信号,它会重新启动工作进程,仍使用旧版配置文件,然后可以将新版Nginx进程杀死
[root@localhost sbin]# kill -HUP 21951

[root@localhost sbin]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Sat, 10 Oct 2020 16:53:22 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 10 Oct 2020 16:28:21 GMT
Connection: keep-alive
ETag: "5f81e125-264"
Accept-Ranges: bytes
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境
    • 下载Nginx
      • 安装依赖包
        • 编译安装旧版本Nginx
          • 编译新版本Nginx
          • Nginx 平滑升级
            • 启动旧版本Nginx
              • 备份旧版本Nginx二进制文件
                • 发送USR2信号
                  • 发送WINCH信号
                    • 版本回滚
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档