Nginx方便地帮助我们实现了平滑升级。其原理简单概括,就是:
1.在不停掉老进程的情况下,启动新进程。
2.老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
3.新进程接受新请求。
4.老进程处理完所有请求,关闭所有连接后,停止。
这样就很方便地实现了平滑升级。一般有两种情况下需要升级Nginx,一种是确实要升级Nginx的版本,另一种是要为Nginx添加新的模块。
目录
Nginx 信号简介
主进程支持的信号
1
2
3
4
5
6
7
8
9
10
11
|
TERM , INT : 立刻退出
QUIT : 等待工作进程结束后再退出
KILL : 强制终止进程
HUP : 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
USR1 : 重新打开日志文件
USR2 : 启动新的主进程,实现热升级
WINCH : 逐步关闭工作进程
工作进程支持的信号
TERM , INT : 立刻退出
QUIT : 等待请求处理结束后再退出
USR1 : 重新打开日志文件
|
升级准备
1.查看现有的 nginx 编译参数
1
2
3
4
5
6
|
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads
|
2.备份原 nginx 二进制文件
备份二进制文件和 nginx 的配置文件期间nginx不会停止服务。
1
|
$ mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.17.0
|
开始升级
1.下载新版nginx源码包
Nginx官方下载地址: https://nginx.org/download/
1
|
$ wget https://nginx.org/download/nginx-1.21.0.tar.gz
|
2.复制新的nginx二进制文件
按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,生产环境中千万不要 make install,如果make install 会将原来的配置文件覆盖。
1
2
3
4
5
6
7
8
9
10
11
12
|
$ tar -zxf nginx-1.21.0.tar.gz
$ cd nginx-1.21.0
$ ./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module --with-threads
--with-threads
$ make
$ cp objs/nginx /usr/local/nginx/sbin/
|
升级前查看进程号,确认进程号与 nginx.pid 一致
1
2
3
4
5
6
7
8
|
$ ps -ef | grep nginx | grep -v grep
root 23136 1 0 21:38 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www 23137 23136 0 21:38 ? 00:00:00 nginx: worker process
www 23138 23136 0 21:38 ? 00:00:00 nginx: worker process
www 23139 23136 0 21:38 ? 00:00:00 nginx: worker process
$ cat /usr/local/nginx/logs/nginx.pid
23136
|
3.执行 make upgrade 命令自动升级
1
2
3
4
5
6
7
8
|
$ make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
|
4.验证是否升级成功
查看 nginx 进程号是否变化(23136-->32682)
1
2
3
4
5
6
7
8
|
$ ps -ef | grep nginx | grep -v grep
root 32682 1 0 22:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www 32683 32682 0 22:27 ? 00:00:00 nginx: worker process
www 32684 32682 0 22:27 ? 00:00:00 nginx: worker process
www 32685 32682 0 22:27 ? 00:00:00 nginx: worker process
$ cat /usr/local/nginx/logs/nginx.pid
32682
|
查看升级后的版本
1
2
3
4
5
6
|
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads
|