Nginx
在企业实际生产环境中经常遇到的一个情况,升级Nginx到新的版本和如何回滚至旧版本。

1、环境介绍

准备两个Nginx版本如下:

  1. [root@nginx ~]# cd /download/nginx/
  2. [root@nginx nginx]# ll
  3. total 1952
  4. -rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz
  5. -rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz

2、编译安装新旧版本

编译安装nginx-1.12.2

  1. [root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz
  2. [root@nginx nginx]# cd nginx-1.12.2
  3. [root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2
  4. [root@nginx nginx-1.12.2]# echo $?
  5. 0
  6. [root@nginx nginx-1.12.2]# make && make install
  7. [root@nginx nginx-1.12.2]# echo $?
  8. 0
  9. [root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/
  10. total 0
  11. drwxr-xr-x 2 root root 333 Mar 1 09:01 conf
  12. drwxr-xr-x 2 root root 40 Mar 1 09:01 html
  13. drwxr-xr-x 2 root root 6 Mar 1 09:01 logs
  14. drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin

编译安装nginx-1.14.2

  1. [root@nginx ~]# cd /download/nginx/
  2. [root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz
  3. [root@nginx nginx]# cd nginx-1.14.2
  4. [root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2
  5. [root@nginx nginx-1.14.2]# echo $?
  6. 0
  7. [root@nginx nginx-1.14.2]# make && make install
  8. [root@nginx nginx-1.14.2]# echo $?
  9. 0
  10. [root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/
  11. total 0
  12. drwxr-xr-x 2 root root 333 Mar 1 09:03 conf
  13. drwxr-xr-x 2 root root 40 Mar 1 09:03 html
  14. drwxr-xr-x 2 root root 6 Mar 1 09:03 logs
  15. drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin

到这里,两个版本的Nginx软件已经部署完成。

3、启动旧版本Nginx

  1. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
  4. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx
  5. [root@nginx ~]# ps -ef|grep nginx
  6. root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
  7. nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
  8. root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx
  9. [root@nginx ~]# lsof -i :80
  10. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  11. nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN)
  12. nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)

4、升级到新版本

版本升级其实就是针对二进制文件的升级,过程如下:

  1. [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
  2. nginx version: nginx/1.12.2
  3. [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
  4. [root@nginx sbin]# mv nginx nginx-1.12.2
  5. #首先备份原来的旧版本nginx二进制文件
  6. [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./
  7. #拷贝新版本的二进制文件到当前目录

接下来进行平滑升级操作

  1. [root@nginx ~]# ps -ef|grep nginx
  2. root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
  3. nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
  4. root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx
  5. [root@nginx ~]# kill -USR2 6324
  6. [root@nginx ~]# ps -ef|grep nginx
  7. root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
  8. nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
  9. root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
  10. nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
  11. root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx

这时新的master进程已经正常开启,但老的work进程也存在,所以使用下面的命令,将老的work进程发出平滑停止的信号,如下:

  1. [root@nginx ~]# kill -WINCH 6324
  2. [root@nginx ~]# ps -ef|grep nginx
  3. root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
  4. root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
  5. nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
  6. root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx

从上面的结果发现,已经平滑的回滚的上一个版本,接下来测试是否能正常访问:
image.png
一样可以正常访问,所以,这个回滚的操作对用户来说也是不可感知的。