简介
注意点
- 当前服务器不关机;
- 用户可以正常访问;
- 由低版本升为高版本;
- 只支持编译安装的nginx;
升级的原理
- 在不停掉老进程的情况下,启动新进程。
- 老进程负责处理仍然没有处理完成的请求,但不接受新请求。
- 新进程接受新的请求。
- 老进程处理完所有请求,关闭所有连接后停止。
升级过程中的信号
主进程支持的信号
- TERM | INT:立刻退出;
- QUIT:等待工作进程结束后再退出;
- KILL:强制终止进程;
- HUP:重新加载配置文件,使用新配置文件启动工作进程,并逐步关闭旧进程;
- USR1:重新打开日志文件;
- USR2:启动新的主进程,实现热升级;
- WINCH:逐步关闭工作进程;
工作进程支持的信号
- TERM | INT:立刻退出;
- QUIT:等待工作进程结束后再退出;
- USR1:重新打开日志文件;
平滑升级过程
升级前准备
检查nginx版本
/usr/local/nginx/sbin/nginx -v#nginx version: nginx/1.12.2
记录nginx使用的模块
/usr/local/nginx/sbin/nginx -V# nginx version: nginx/1.12.2
# built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
# built with OpenSSL 1.0.2k-fips 26 Jan 2017
# TLS SNI support enabled
# configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
记录nginx的PID文件路径
ll /usr/local/nginx/logs/nginx.pid
备份nginx
mkdir -p /data/nginx_bak
tar -cvf /data/nginx_bak/nginx1.12.2.tgz /usr/local/nginx
安装新nginx版本
下载nginx
wget https://nginx.org/download/nginx-1.24.0.tar.gz
解压nginx文件
tar -zxvf nginx-1.24.0.tar.gz -C /usr/local/
安装新nginx
cd /usr/local/nginx-1.24.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
#注意:这里不能进行,make install 操作,否则将会被覆盖,可能会影响线上业务。
make
替换二进制文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp /usr/local/nginx-1.24.0/objs/nginx /usr/local/nginx/sbin/#检查新版本nginx
/usr/local/nginx/sbin/nginx -t
升级nginx版本
#查看nginx旧版本进程
ps -ef | grep nginx
# root 4731 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 4732 4731 0 16:04 ? 00:00:00 nginx: worker process
# root 7436 1151 0 16:22 pts/0 00:00:00 grep --color=auto nginx#向主进程发送USR2信号
#(Nginx会启动一个新版本的master进程和工作进程,和旧版一起处理请求)
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`#查看nginx进程
ps -ef | grep nginx
# root 4731 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 4732 4731 0 16:04 ? 00:00:00 nginx: worker process
# root 7439 4731 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 7440 7439 0 16:23 ? 00:00:00 nginx: worker process
# root 7442 1151 0 16:24 pts/0 00:00:00 grep --color=auto nginx#向原主进程发送WINCH信号
ll /usr/local/nginx/logs/nginx.pid.oldbin
kill -WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin`#查看nginx进程
ps -ef | grep nginx
# root 4731 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# root 7439 4731 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 7440 7439 0 16:23 ? 00:00:00 nginx: worker process
# root 7451 1151 0 16:29 pts/0 00:00:00 grep --color=auto nginx#向原主机发送QUIT信号
ll /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`#查看nginx进程
ps -ef | grep nginx
# root 7439 1 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 7440 7439 0 16:23 ? 00:00:00 nginx: worker process
# root 7454 1151 0 16:31 pts/0 00:00:00 grep --color=auto nginx
查看nginx
/usr/local/nginx/sbin/nginx -t
#重点检查nginx版本
/usr/local/nginx/sbin/nginx -v
/usr/local/nginx/sbin/nginx -V
添加新模块过程
添加前准备
检查nginx版本
/usr/local/nginx/sbin/nginx -v#nginx version: nginx/1.12.2
记录nginx使用的模块
/usr/local/nginx/sbin/nginx -V# nginx version: nginx/1.12.2
# built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
# built with OpenSSL 1.0.2k-fips 26 Jan 2017
# TLS SNI support enabled
# configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
记录nginx的PID文件路径
ll /usr/local/nginx/logs/nginx.pid
备份nginx
mkdir -p /data/nginx_bak
tar -cvf /data/nginx_bak/nginx1.12.2.tgz /usr/local/nginx
添加新模块
添加新模块
cd /usr/local/nginx-1.12.2/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_gzip_static_module
#注意:这里不能进行,make install 操作,否则将会被覆盖,可能会影响线上业务。
make
替换二进制文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp /usr/local/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin/#检查新版本nginx
/usr/local/nginx/sbin/nginx -t
平滑替换二进制文件
#查看nginx旧版本进程
ps -ef | grep nginx
# root 4731 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 4732 4731 0 16:04 ? 00:00:00 nginx: worker process
# root 7436 1151 0 16:22 pts/0 00:00:00 grep --color=auto nginx#向主进程发送USR2信号
#(Nginx会启动一个新版本的master进程和工作进程,和旧版一起处理请求)
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`#查看nginx进程
ps -ef | grep nginx
# root 4731 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 4732 4731 0 16:04 ? 00:00:00 nginx: worker process
# root 7439 4731 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 7440 7439 0 16:23 ? 00:00:00 nginx: worker process
# root 7442 1151 0 16:24 pts/0 00:00:00 grep --color=auto nginx#向原主进程发送WINCH信号
ll /usr/local/nginx/logs/nginx.pid.oldbin
kill -WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin`#查看nginx进程
ps -ef | grep nginx
# root 4731 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# root 7439 4731 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 7440 7439 0 16:23 ? 00:00:00 nginx: worker process
# root 7451 1151 0 16:29 pts/0 00:00:00 grep --color=auto nginx#向原主机发送QUIT信号
ll /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`#查看nginx进程
ps -ef | grep nginx
# root 7439 1 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
# nginx 7440 7439 0 16:23 ? 00:00:00 nginx: worker process
# root 7454 1151 0 16:31 pts/0 00:00:00 grep --color=auto nginx
检查nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -v
#重点检查nginx模块
/usr/local/nginx/sbin/nginx -V