Nginx平滑升级及添加功能
文章目录
- Nginx平滑升级及添加功能
- 1-为什么要对 nginx 平滑升级
- 2-nginx 平滑升级描述
- 2-1Nginx信号简介
- 3-1查看现阶段Nginx编译参数
- 3-2下载新版本的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等。
2-1Nginx信号简介
主进程支持的信号
- TERM, INT: 立刻退出
- QUIT: 等待工作进程结束后再退出
- KILL: 强制终止进程
- HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
- USR1: 重新打开日志文件
- USR2: 启动新的主进程,实现热升级
- WINCH: 逐步关闭工作进程
工作进程支持的信号
- TERM, INT: 立刻退出
- QUIT: 等待请求处理结束后再退出
- USR1: 重新打开日志文件
#3-Nginx平滑升级部署及添加功能
3-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 ~]#
3-2下载新版本的Nginx源码包和扩展模板
Nginx-1-22.0下载nginx_module_echo下载按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖
//下载git
[root@Masters ~]# yum -y install git
//下载扩展包
[root@Masters ~]# cd /usr/src/
[root@Masters src]# 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.19 MiB/s, done.
[root@Masters src]# ls
debug kernels nginx-1.20.2 nginx-1.20.2.tar.gz nginx-1.22.0.tar.gz nginx_module_echo
[root@Masters src]# tar xf nginx-1.22.0.tar.gz
[root@Masters src]# cd nginx-1.22.0/
[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@Masters 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
//安装信息对比
[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]#
//平滑升级
[root@Masters nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx;cp objs/nginx /usr/local/nginx/sbin/nginx;systemctl start nginx
cp: overwrite '/usr/local/nginx/sbin/nginx'? y
[root@Masters nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0
//查看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:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*