nginx平滑升级及添加功能

1、为什么要对 nginx 平滑升级

随着 nginx 越来越流行,并且 nginx 的优势也越来越明显,nginx 的版本迭代也来时加速模式,nginx更新了许多新功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就需要平滑升级。

nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。
这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。

2、nginx 平滑升级描述

多进程模式下的请求分配方式

nginx 默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接

信号的接收和处理

nginx 主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。

Nginx信号简介

主进程支持的信号

  • TERM, INT: 立刻退出
  • QUIT: 等待工作进程结束后再退出
  • KILL: 强制终止进程
  • HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
  • USR1: 重新打开日志文件
  • USR2: 启动新的主进程,实现热升级
  • WINCH: 逐步关闭工作进程

工作进程支持的信号

  • TERM, INT: 立刻退出
  • QUIT: 等待请求处理结束后再退出
  • USR1: 重新打开日志文件

3、nginx 平滑升级及添加新功能实战

1、查看现有的 nginx 编译参数,

[root@Masters ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@Masters ~]# 

2.上传新版本的源码包nginx-1.22.0.tar.gz,添加扩展模板

https://gitee.com/wujunze/nginx_module_echo?_from=gitee_search
在这里插入图片描述

按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖

[root@Masters ~]# dnf -y install git
[root@Masters ~]# git clone https://gitee.com/wujunze/nginx_module_echo.git
Cloning into 'nginx_module_echo'...
remote: Enumerating objects: 80, done.
remote: Total 80 (delta 0), reused 0 (delta 0), pack-reused 80
Unpacking objects: 100% (80/80), 14.32 KiB | 1.43 MiB/s, done.
[root@Masters ~]# ls
anaconda-ks.cfg    nginx_module_echo
nginx-1.22.0.tar.gz  
[root@Masters ~]# tar xf nginx-1.22.0.tar.gz 
[root@Masters ~]# cd nginx-1.22.0/
[root@Masters nginx-1.22.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

[root@Masters nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../nginx_module_echo

## 这里去除了日志信息安装添加了--add-module=../nginx_module_echo
##千万不要make install 
[root@server nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)

##objs/nginx -v是新的版本,版本信息对比
[root@Masters nginx-1.22.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README

[root@Masters nginx-1.22.0]# objs/nginx -v
nginx version: nginx/1.22.0
[root@Masters nginx-1.22.0]# nginx -v
nginx version: nginx/1.20.2

#这是一条直接执行平滑升级的命令,这个命令可以非常快的升级,是所有命令的总和,是为了防止暂停nginx时影响
#服务器运行的命令,可以直接使用这个就行了
[root@server nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx;cp objs/nginx /usr/local/nginx/sbin/nginx;systemctl start nginx

###如下是操作的意思介绍
#安装信息对比
[root@Masters nginx-1.22.0]# objs/nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../nginx_module_echo

[root@Masters nginx-1.22.0]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@Masters nginx-1.22.0]# 

#配置文件的大小对比,证明不是一个文件,这里新配置文件小是因为去除的日志信息
[root@Masters nginx-1.22.0]# ll objs/nginx
-rwxr-xr-x 1 root root 6227816 Oct 12 03:20 objs/nginx
[root@Masters nginx-1.22.0]# ll /usr/local/nginx/sbin/nginx 
-rwxr-xr-x 1 root root 6314560 Oct 10 12:14 /usr/local/nginx/sbin/nginx
[root@Masters nginx-1.22.0]# 

#暂停nginx进程
[root@Masters nginx-1.22.0]# pkill nginx
[root@Masters nginx-1.22.0]# ss -anlt
State      Recv-Q     Send-Q         Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                  0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                     [::]:22                     [::]:*                    
[root@Masters nginx-1.22.0]# 

#修改安装的配置文件,把新的配置文件信息复制到安装目录当中
[root@Masters nginx-1.22.0]# mv /usr/local/nginx/sbin/nginx{,-bak}
[root@Masters nginx-1.22.0]# ls /usr/local/nginx/sbin/
nginx-bak
[root@Masters nginx-1.22.0]# cp objs/nginx /usr/local/nginx/sbin/
[root@Masters nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0

##重新启动nginx服务
[root@Masters nginx-1.22.0]# systemctl start nginx.service 
[root@Masters nginx-1.22.0]# ss -anlt
State      Recv-Q     Send-Q         Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                  0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                  0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                     [::]:22                     [::]:*                    
[root@Masters nginx-1.22.0]# 

浏览器访问测试是否可以访问

***
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值