1. 升级
1.1. 下载、编译新版本的 Nginx
➜ ~ wget https://nginx.org/download/nginx-1.15.6.tar.gz
➜ ~ tar zxvf nginx-1.15.6.tar.gz
➜ ~ cd nginx-1.15.6
➜ ~ ./configure --prefix=/usr/local/nginx
➜ ~ make
备份原 Nginx 二进制文件,并用新版本替换
➜ ~ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
➜ ~ \cp -rf objs/nginx /usr/local/nginx/sbin/nginx
1.2. 切换 Nginx 的新老进程
➜ ~ cd /usr/local/nginx/sbin/
➜ ~ ps -ef | grep nginx
root 6680 1 0 12:17 ? 00:00:00 nginx: master process ./nginx
nobody 6681 6680 0 12:17 ? 00:00:00 nginx: worker process
root 31579 3601 0 12:28 pts/0 00:00:00 grep --color=auto nginx
向正在运行的老版本的 master
进程发送热部署信号
➜ ~ kill -USR2 6680
# Nginx 将使用新的二进制文件启动新版本的 master 和 worker 进程
# 并将所有用户请求平滑的过渡到新的进程
➜ ~ ps -ef | grep nginx
root 6680 1 0 12:17 ? 00:00:00 nginx: master process ./nginx
nobody 6681 6680 0 12:17 ? 00:00:00 nginx: worker process
root 31581 6680 0 12:29 ? 00:00:00 nginx: master process ./nginx
nobody 31582 31581 0 12:29 ? 00:00:00 nginx: worker process
root 31584 3601 0 12:29 pts/0 00:00:00 grep --color=auto nginx
向老版本的 master
进程发送信号,让其优雅关闭所有 worker
进程
➜ ~ kill -WINCH 6680
➜ ~ ps -ef | grep nginx
root 6680 1 0 12:17 ? 00:00:00 nginx: master process ./nginx
root 31581 6680 0 12:29 ? 00:00:00 nginx: master process ./nginx
nobody 31582 31581 0 12:29 ? 00:00:00 nginx: worker process
root 31587 3601 0 12:31 pts/0 00:00:00 grep --color=auto nginx
⚠️ 注意:老版本的
master
进程不会自动退出,以方便执行版本回退操作。当新版本稳定运行后,再发送信号退出老版本的master
进程。
<!-- more -->
2. 版本回退
-
向老版本的 Nginx 进程发送信号,重启其
worker
进程➜ ~ kill -HUP 6680 ➜ ~ ps -ef | grep nginx root 6680 1 0 12:17 ? 00:00:00 nginx: master process ./nginx root 31581 6680 0 12:29 ? 00:00:00 nginx: master process ./nginx nobody 31582 31581 0 12:29 ? 00:00:00 nginx: worker process nobody 31588 6680 0 12:35 ? 00:00:00 nginx: worker process root 31590 3601 0 12:35 pts/0 00:00:00 grep --color=auto nginx
-
等老版本的 Nginx
worker
进程启动后,向新版本的 Nginxmaster
进程发送信号,让其退出打开的所有worker
进程➜ ~ kill -WINCH 31581 ➜ ~ ps -ef | grep nginx root 6680 1 0 12:17 ? 00:00:00 nginx: master process ./nginx root 31581 6680 0 12:29 ? 00:00:00 nginx: master process ./nginx nobody 31588 6680 0 12:35 ? 00:00:00 nginx: worker process root 31590 3601 0 12:35 pts/0 00:00:00 grep --color=auto nginx
-
最后退出新版本的
master
进程➜ ~ kill -QUIT 31581 ➜ ~ ps -ef | grep nginx root 6680 1 0 12:17 ? 00:00:00 nginx: master process ./nginx nobody 31588 6680 0 12:35 ? 00:00:00 nginx: worker process root 31592 3601 0 12:36 pts/0 00:00:00 grep --color=auto nginx