Nginx平滑升级与版本回滚操作指南
在企业实际生产环境中,经常面临将Nginx升级至新版本或在出现问题时回滚至旧版本的需求。
环境介绍
本次实验涉及的两个Nginx版本分别为:
[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# ll
total 1952
-rw-r--r-- 1 root root 981687 Oct 17 2026 nginx-1.12.2.tar.gz
-rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz
编译安装新旧版本
首先编译安装nginx-1.12.2
[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz
[root@nginx nginx]# cd nginx-1.12.2
[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:01 conf
drwxr-xr-x 2 root root 40 Mar 1 09:01 html
drwxr-xr-x 2 root root 6 Mar 1 09:01 logs
drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin
接着编译安装nginx-1.14.2
[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz
[root@nginx nginx]# cd nginx-1.14.2
[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# make && make install
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:03 conf
drwxr-xr-x 2 root root 40 Mar 1 09:03 html
drwxr-xr-x 2 root root 6 Mar 1 09:03 logs
drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin
至此,两个版本的Nginx软件均已部署完毕。
启动旧版本Nginx
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN)
nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)
执行启动命令
版本升级的核心在于二进制文件的替换,具体流程如下:
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.12.2
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.12.2
#首先备份原来的旧版本nginx二进制文件
[root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./
#拷贝新版本的二进制文件到当前目录
接下来执行平滑升级操作
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# kill -USR2 6324
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx
此时新的master进程已正常启动,但旧的work进程仍在运行。需使用以下命令向旧work进程发送平滑停止信号,具体如下:
[root@nginx ~]# kill -WINCH 6324
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx
旧work进程已停止,接下来测试服务是否正常访问:

服务可正常访问。由于该平滑升级动作对用户完全无感知,因此Nginx的热部署已完成。
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.14.2
查看版本号确认为最新版本,升级成功。
注:若在版本升级完成后确认无误,需关闭旧master进程,可使用以下命令:
kill -QUIT old_master_PID
版本回滚
对于升级而言,难点往往不在于升级本身,而在于回滚。因为在实际生产环境中,回滚是可能发生的场景,例如新版本存在未知bug导致与应用不兼容,或出现运行不稳定等情况。
因此,故障回滚能力是运维工程师的重点技能。
在上述结果中可以看到,旧的master进程一直存在,在未手动关闭前不会自动退出。这种设计具有积极意义,其好处在于当新版本升级后出现问题时,能够及时、快速地回滚至上一个稳定版本。
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6350 1244 0 09:23 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.14.2
[root@nginx sbin]# mv nginx-1.12.2 nginx
[root@nginx sbin]# kill -USR1 6324
[root@nginx sbin]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx
[root@nginx sbin]# ./nginx -v
nginx version: nginx/1.12.2
从上述结果发现,已成功平滑回滚至上一版本,接下来测试是否能正常访问:

同样可以正常访问,说明该回滚操作对用户也是不可感知的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。