nginx热升级也就是在不关闭nginx服务的情况下,对nginx做一个升级更新。(升级nginx的版本,不暂停nginx上的web服务)
如果只是简单的对nginx的配置文件进行了修改,那么进行一个reload就可以了,如果添加新的模块,或者升级新的版本,就需要进行热升级。并且要参考以前编译的模块,把模块信息预编译进去,如果不添加,那么以前的模块就不能使用了
在这里,我的nginx是使用编译安装的,之前安装的具体脚本,可以看我的另外一篇文章,里面有详细的一件部署安装脚本:Nginx的相关内容,以及配置文件详解_万物皆可爱33的博客-CSDN博客_nginx配置文件内容
具体步骤:
1. 首先修改旧的nginx的二进制程序的名字
[root@xieshan sbin]# pwd #编译安装脚本中指定了配置的路径是/usr/local/sclilin99
/usr/local/sclilin99/sbin
[root@xieshan sbin]# ls
nginx
[root@xieshan sbin]# mv nginx nginx.old
[root@xieshan sbin]# ls
nginx.old
2.安装新版本的nginx,解压,并且对新版本的nginx进行编译
[root@xieshan lilin99]# cd /lilin99
[root@xieshan lilin99]# ls
nginx-1.21.4 nginx-1.21.4.tar.gz
[root@xieshan lilin99]# wget https://nginx.org/download/nginx-1.23.1.tar.gz #安装最新版本的nginx
[root@xieshan lilin99]# ls
nginx-1.21.4 nginx-1.23.1.tar.gz nginx-1.21.4.tar.gz
[root@xieshan lilin99]# tar xf nginx-1.23.1.tar.gz
[root@xieshan lilin99]# ls
nginx-1.21.4 nginx-1.23.1.tar.gz nginx-1.21.4.tar.gz nginx-1.23.1
[root@xieshan lilin99]# cd nginx-1.23.1
[root@xieshan nginx-1.23.1]# ./configure --prefix=/usr/local/sclilin99 --user=lilin --group=lilin --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_geoip_module --with-http_gunzip_module #编译前的配置,跟旧版本nginx的一摸一样
[root@xieshan nginx-1.23.1]# make
3.将新的nginx二进制程序复制到原来旧文件的位置,并且取代
[root@xieshan nginx-1.23.1]# cp objs/nginx /usr/local/sclilin99/sbin/
[root@xieshan nginx-1.23.1]# cd /usr/local/sclilin00/sbin
[root@xieshan sbin]# ls
nginx nginx.old
[root@xieshan nginx-1.23.1]# ps aux|grep nginx #此时启动的还是nginx的旧版本
root 19466 0.0 0.1 50632 1312 ? Ss 20:33 0:00 nginx: master process /usr/local/sclilin99/sbin/nginx
lilin 19467 0.0 0.2 51548 2332 ? S 20:33 0:00 nginx: worker process
lilin 19468 0.0 0.2 51548 2836 ? S 20:33 0:00 nginx: worker process
root 22552 0.0 0.0 112824 984 pts/3 R+ 20:41 0:00 grep --color=auto nginx
[root@xieshan nginx-1.23.1]# /usr/local/sclilin99/sbin/nginx -s reload #重新加载配置文件
[root@xieshan nginx-1.23.1]# ps aux|grep nginx #此时启动的是nginx的最新版本了,注意,master的进程号没有变,更改的是worker的进程号
root 19466 0.0 0.2 51436 2512 ? Ss 20:33 0:00 nginx: master process /usr/local/sclilin99/sbin/nginx
lilin 22554 0.0 0.2 51772 2548 ? S 20:42 0:00 nginx: worker process
lilin 22555 0.0 0.2 51772 2548 ? S 20:42 0:00 nginx: worker process
root 22557 0.0 0.0 112824 988 pts/3 R+ 20:42 0:00 grep --color=auto nginx
[root@xieshan nginx-1.23.1]# /usr/local/sclilin99/sbin/nginx -v
nginx version: nginx/1.23.1
网上还有很多关于热升级的文章,里面牵扯到的给master发送USR2信号,我没有看到具体如何操作的。我这个方法虽然没有发送信号,但是也在不关闭服务的情况下,成功的更新nginx的版本!整个过程中nginx的master没有改变,进程号还是和原来一样,但是worker发生了变化。在产生新的master之后,master会将web服务的请求都分配给新的worker,然后旧的worker自动退出。
热升级过程中,请记得备份,这样如果更新失败了,也方便回滚!
#此文章仅供参考,如果有其他更好的实现方法,欢迎指正!