Nginx 的平滑升级
- 平滑升级概述、参数
- 1、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 等。
- 3、Nginx 信号简介
主进程支持的信号
TERM, INT: 立刻退出
QUIT: 等待工作进程结束后再退出
KILL: 强制终止进程
HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
USR1: 重新打开日志文件
USR2: 启动新的主进程,实现热升级
WINCH: 逐步关闭工作进程
工作进程支持的信号
TERM, INT: 立刻退出
QUIT: 等待请求处理结束后再退出
USR1: 重新打开日志文件
- Nginx 平滑升级操作
1、查看现有的 nginx 编译参数
[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 May 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
2.上传新版本的源码包 nginx-1.19.10.tar.gz,解压到/usr/local/
按照原来的编译参数安装 nginx 的方法进行安装,然后make,不要 make install(make install 会将原来的配置文件覆盖)
[root@nginx-server2 ~]# cd /usr/local/nginx-1.19.10/
[root@nginx-server2 nginx-1.19.10]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@nginx-server2 nginx-1.19.10]# make
#千万不要 make install !!
3、备份原 nginx 二进制文件
备份二进制文件和 nginx 的配置文件(期间 nginx 不会停止服务)
[root@nginx-server2 nginx-1.19.10]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_$(date +%F)
4、复制新的 nginx 二进制文件,进入新的 nginx 源码包
[root@nginx-server2 nginx-1.19.10]# cp /usr/local/nginx-1.19.10/objs/nginx /usr/local/nginx/sbin/
5、测试新版本的 nginx 是否正常
[root@nginx-server2 nginx-1.19.10]# /usr/local/nginx/sbin/nginx -t
6、给 nginx 发送平滑迁移信号(若不清楚 pid 路径,请查看 nginx 配置文件)
USR2: 启动新的主进程,实现热升级
[root@nginx-server2 nginx-1.19.10]# kill -USR2 `cat /var/run/nginx.pid`
7、查看 nginx pid,会出现一个 nginx.pid.oldbin
[root@nginx-server2 nginx-1.19.10]# ll /var/run/nginx.pid*
8、从容关闭旧的 Nginx 进程(WINCH: 逐步关闭工作 work 进程)
[root@nginx-server2 nginx-1.19.10]# kill -WINCH `cat /var/run/nginx.pid.oldbin`
9、此时不重载配置启动旧的工作进程
HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
[root@nginx-server2 nginx-1.19.10]# kill -HUP `cat /var/run/nginx.pid.oldbin`
10、结束工作进程,完成此次升级
QUIT: 等待请求处理结束后再退出
[root@nginx-server2 nginx-1.19.10]# kill -QUIT `cat /var/run/nginx.pid.oldbin`
11、验证 Nginx 是否升级成功
[root@nginx-server2 nginx-1.19.10]# /usr/local/nginx/sbin/nginx -V
12、访问验证
elinks 10.8.165.121
PS. 使用yum安装Nginx 的平滑升级方式一样,下载新的安装包解压到同一目录配置、make即可。
- Nginx 错误页面配置
nginx 错误页面包括 404 403 500 502 503 504 等页面,在 server 中增加配置即可:
#error_page 404 403 500 502 503 504 /404.html;
location = /404.html {
root /usr/local/nginx/html;
}
注意:
/usr/local/nginx/html/ 路径下必须有 404.html 文件
404.html 上如果引用其他文件的 png 或 css 就会有问题,无法显示,因为其他文件的访问也需要做配置;
为了简单,可以将 css 嵌入文件中,图片用 base 编码嵌入;如下:
[root@localhost html]# vim 404.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>404-[对不起!您访问的页面不存在] </title>
<style type="text/css">
.head404{ width:580px; height:234px; margin:50px auto 0 auto; background:url('http://zq027.com/Public/images/head404.png') no-repeat; }
.txtbg404{ width:499px; height:169px; margin:10px auto 0 auto; background:url('http://zq027.com/Public/images/txtbg404.png') no-repeat;}
.txtbg404 .txtbox{ width:390px; position:relative; top:30px; left:60px;color:#eee; font-size:13px;}
.txtbg404 .txtbox p {margin:5px 0; line-height:18px;}
.txtbg404 .txtbox .paddingbox { padding-top:15px;}
.txtbg404 .txtbox p a { color:#eee; text-decoration:none;}
.txtbg404 .txtbox p a:hover { color:#FC9D1D; text-decoration:underline;}
</style>
</head>
<body bgcolor="#494949">
<div class="head404"></div>
<div class="txtbg404">
<div class="txtbox">
<p>对不起,您请求的页面不存在、或已被删除、或暂时不可用</p>
<p class="paddingbox">请点击以下链接继续浏览网页</p>
<p>》<a style="cursor:pointer" οnclick="history.back()">返回上一页面</a></p>
<p>》<a href="/">返回网站首页</a></p>
</div>
</div>
</body>
</html>
展示效果:


1244

被折叠的 条评论
为什么被折叠?



