如何对nginx进行平滑升级

nginx平滑升级,不影响线上业务的运行

这里nginx的安装目录是:nginx

第一步:查看nginx版本号:

[root@localhost nginx-web]# nginx -v
nginx version: nginx/1.15.12

这里的版本号是1.10.3,对比下官网的版本号:

第二步:下载上传并解压nginx安装包

我这里下载的是1.21.4的版本号 nginx-1.21.4,下载下来后上传到服务器,

下载地址:nginx: download

解压命令

tar zxvf nginx-1.21.4.tar.gz

查看解压后的文件  ,

[root@localhost nginx_new]# ls -l
total 1048
drwxr-xr-x 9 vsftpd vsftpd     186 Dec 20 17:47 nginx-1.21.4
-rw-r--r-- 1 root   root   1070260 Dec 20 17:05 nginx-1.21.4.tar.gz

[root@localhost nginx_new]# cd nginx-1.21.4/
[root@localhost nginx-1.21.4]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

 第三步:编译安装包

可以看到解压后的目录下有  configure

1、 ./configure 是配置相关参数

        这里需要了解之前的安装配置

        1、如果配置了nginx全局启动可以使用 nginx -V查看

        2、如果没有设置全局启动,到nginx安装目录下 找到sbin文件夹,进入后 使用: ./nginx -V

        -v  查看版本号

        -V 查看安装细节 

        在"./configure"配置中,"--with"表示启用模块,也就是说这些模块在编译时不会自动构建"--without"表示禁用模块,也就是说这些模块在编译时会自动构建,若你想Nginx轻量级运行,可以去除一些不必要的模块。

        这里可以看到的是 之前的安装配置比较复杂比较全,直接将  --prefix=一直到最后复制下来,然后使用./configure 执行 

./configure --prefix=/usr/local/nginx-web --with-http_ssl_module --with-http_gzip_static_module

如果,配置错了需要重新配置编译,这里可以重复执行

 2、使用make命令进行编译 

        这里是升级不要使用make install,只编译不安装

[root@localhost nginx-1.21.4]# make

看到这里说明编译成功,编译好的文件在 objs目录下

测试编译后的程序是否可行:

执行:objs/nginx -t -c /usr/local/nginx-web/conf/nginx.conf,结果如下图

 第四步:替换原有的nginx启动程序

1、替换nginx的启动程序之前先做好备份

[root@localhost nginx-web]# mv /usr/local/nginx-web/sbin/nginx /usr/local/nginx-web/sbin/nginx.15.old

 2、查看备份结果,备份成功,现在开始准备替换nginx

 也可以把配置文件nginx.conf备份下

编译好的程序在objs目录下:

 3、执行替换,使用cp复制到对应的目录下就好

 这里替换命令报错,因为我们的nginx正处于启动状态

4、查看下 ps -ef | grep nginx,,这里看到进程是10441

 第一种:直接kill 干掉进程,进行替换

第二种:发送USR2信号给旧版本主进程号,使nginx的旧版本停止接收请求,用nginx新版本接替,且老进程处理完所有请求,关闭所有连接后,停止后替换

执行命令 kill -USR2 `cat nginx.pid`

kill -USR2 `cat nginx.pid`

查看nginx pid目录,多了个nginx.pid.oldbin文件,存放了旧版本nginx的pid号 

kill -WINCH `cat nginx.pid.oldbin`

从容关闭旧进程:kill -QUIT `cat nginx.pid.oldbin`

kill -QUIT `cat nginx.pid.oldbin`

[root@localhost logs]# kill -QUIT `cat nginx.pid.oldbin`
[root@localhost logs]# ls
access.log  error.log  nginx.pid

5、查看版本号 ./nginx -v

[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.21.4

6、执行./nginx -s relaod

7、检查是否启动成功

[root@localhost logs]# ps -ef | grep nginx
nobody    3976 27438  0 11:18 ?        00:00:00 nginx: worker process
root      5224 12257  0 11:19 pts/1    00:00:00 grep --color=auto nginx
root     27438     1  0 10:36 ?        00:00:00 nginx: master process ./nginx
[root@localhost logs]# ll /proc/27438/exe
lrwxrwxrwx 1 root root 0 12月 21 10:41 /proc/27438/exe -> /usr/local/nginx-web/sbin/nginx.15.old
[root@localhost logs]#  /usr/local/nginx-web/sbin/nginx.15.old -v
nginx version: nginx/1.15.12

 可以看到的是这里依然使用的是旧程序。

可以执行以下命令:


[root@localhost logs]# kill -USR2 `cat nginx.pid`
[root@localhost logs]# ls
access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@localhost logs]# kill -WINCH `cat nginx.pid.oldbin`

查看结果:

kill -USR2 的作用: 新执行文件启动,生成新进程号,新旧的进程共存! 

所以运行 kill -USER2之后,存在nginx.pid 和 nginx.pid.oldbin

kill -WINCH 的作用:新来的请求统统由新程序处理,老的连接还继续由老程序处理;上图的结果印证这一说法;

稍等一段时间后,感觉没有人使用旧程序了,这个时候,时机到了,彻底弃用旧程序;

这个时候再执行:kill -QUIT `cat nginx.pid.oldbin`  ,干掉旧的程序

kill -QUIT `cat nginx.pid.oldbin` 

这个时候查看的结果如下,说明已经转到新程序了

说明:如何查找nginx的目录

方法一:

如果找不到nginx安装目录,可以使用 nginx -V寻找nginx的目录 ,前提是服务器中的nginx是全局命令,可以这样查找

方法二:

也可以使用:whereis nginx

方法三 使用 ps -ef | grep nginx  和 ll /proc/{PID}/exe

希望能帮助到你,记得点赞!!!谢谢!!!

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>