# Nginx 平滑升级
# 1.Nginx 平滑升级概述
# 1.1 什么是平滑升级
在进行服务版本升级的时候,对于用户访问体验无感知、不会造成服务中断。
# 1.2 平滑升级实现原理
![2.jpg]()
# 1.3 平滑升级实现思路
如何实现 Nginx 平滑升级思路(建议准备一个大文件持续下载验证升级是否会影响业务)
- 1. 下载新版本 nginx
- 2. 了解原旧版本 nginx 编译参数
- 3. 将旧 nginx 二进制文件进行备份,然后替换成为新的 nginx 二进制文件
- 4. 向旧 nginx 的 Master 进程发送 USR2 信号
- 旧 master 进程的 pid 文件添加后缀.oldbin
- master 进程会用新 nginx 二进制文件启动新的 master 进程
- 5. 向旧 master 进程发送 WINCH 信号,旧的 worker 子进程优雅退出
- 6. 向旧 master 进程发送 QUIT 信号,旧的 master 进程就退出了
# 1.4 平滑升级所需信号
信号 | 含义 |
---|
QUIT | 优雅关闭、quit |
HUP | 优雅重启、reload |
USR1 | 重新打开日志文件、reopen |
USR2 | 平滑升级可执行的二进制程序 |
WINCH | 平滑关闭 Worker 进程 |
# 2. Nginx 平滑升级实践
# 2.1 安装 Nginx 所需依赖
| [root@proxy01 ~] |
| libxslt-devel gd-devel perl-ExtUtils-Embed \ |
| geoip-devel gperftools-devel pcre-devel openssl-devel -y |
# 2.2 编译并安装 Nginx
| |
| [root@proxy01 ~] |
| [root@proxy01 ~] |
| [root@proxy01 ~] |
| |
| |
| [root@proxy01 nginx-1.29.1] |
| |
| |
| [root@proxy01 ~] |
| |
| |
| [[root@proxy01 ~] |
# 2.3 备份旧 Nginx 二进制
| |
| [root@proxy01 nginx-1.29.1] |
| [root@proxy01 nginx-1.29.1] |
| [root@proxy02 nginx-1.29.1] |
| -rwxr-xr-x 1 root root 10542864 Sep 8 20:44 /usr/sbin/nginx |
| -rwxr-xr-x 1 root root 1530552 May 30 2024 /usr/sbin/nginx-debug |
| -rwxr-xr-x 1 root root 1407480 May 30 2024 /usr/sbin/nginx.old |
# 2.4 向旧 Master 发送 USR2 信号
| |
| [root@proxy02 nginx-1.29.1] |
| root 1610 0.0 0.0 59448 1240 ? Ss 18:16 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 1611 0.0 0.2 60032 3740 ? S 18:16 0:00 nginx: worker process |
| nginx 1612 0.0 0.1 59876 1972 ? S 18:16 0:00 nginx: worker process |
| root 5674 0.0 0.0 112812 980 pts/0 S+ 20:45 0:00 grep --color=auto nginx |
| |
| |
| [root@proxy02 nginx-1.29.1] |
| |
| |
| [root@proxy02 nginx-1.29.1] |
| 1610 |
| |
| |
| [root@proxy02 nginx-1.29.1] |
| root 1610 0.0 0.0 59448 1240 ? Ss 18:16 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 1611 0.0 0.2 60032 3740 ? S 18:16 0:00 nginx: worker process |
| nginx 1612 0.0 0.1 59876 1972 ? S 18:16 0:00 nginx: worker process |
| root 5676 0.0 0.2 56960 4040 ? S 20:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5677 0.0 0.1 57404 1972 ? S 20:47 0:00 nginx: worker process |
| nginx 5678 0.0 0.1 57404 1972 ? S 20:47 0:00 nginx: worker process |
| root 5683 0.0 0.0 112812 980 pts/0 S+ 20:48 0:00 grep --color=auto nginx |
![1.jpg]()
# 2.5 向旧 Master 发送 WINCH 信号
| |
| [root@proxy02 nginx-1.29.1] |
| [root@proxy02 nginx-1.29.1] |
| root 1610 0.0 0.0 59448 1396 ? Ss 18:16 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| root 5676 0.0 0.2 56960 4040 ? S 20:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5677 0.0 0.1 57404 1972 ? S 20:47 0:00 nginx: worker process |
| nginx 5678 0.0 0.1 57404 3480 ? S 20:47 0:00 nginx: worker process |
| root 5702 0.0 0.0 108096 616 pts/1 S+ 20:51 0:00 tail -f /var/log/nginx/error.log |
| root 5712 0.0 0.0 112812 980 pts/0 S+ 20:55 0:00 grep --color=auto nginx |
# 2.6 向旧 Master 发送 QUIT 信号
| [root@proxy02 nginx-1.29.1] |
| [root@proxy02 nginx-1.29.1] |
| root 5676 0.0 0.2 56960 4040 ? S 20:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5677 0.0 0.1 57404 1972 ? S 20:47 0:00 nginx: worker process |
| nginx 5678 0.0 0.1 57404 3480 ? S 20:47 0:00 nginx: worker process |
| root 5702 0.0 0.0 108096 616 pts/1 S+ 20:51 0:00 tail -f /var/log/nginx/error.log |
| root 5714 0.0 0.0 112812 980 pts/0 S+ 20:57 0:00 grep --color=auto nginx |
# 3. Nginx 平滑回滚实践
# 3.1 平滑回退思路
- 1. 替换 nginx 二进制文件
- 2. 向旧的 1.21 master 发送 USR2 信号
- 3. 向旧的 1.21 master 发送 WINCH
- 4. 向旧的 1.21 master 发送 QUIT
# 3.2 替换 nginx 二进制文件
| [root@proxy02 nginx-1.29.1] |
| [root@proxy02 nginx-1.29.1] |
# 3.3 向旧的 master 发送 USR2 信号
| [root@proxy02 nginx-1.29.1] |
| root 5676 0.0 0.2 56960 4040 ? S 20:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5677 0.0 0.1 57404 1972 ? S 20:47 0:00 nginx: worker process |
| nginx 5678 0.0 0.1 57404 3480 ? S 20:47 0:00 nginx: worker process |
| root 5702 0.0 0.0 108096 616 pts/1 S+ 20:51 0:00 tail -f /var/log/nginx/error.log |
| root 5743 0.0 0.0 112812 980 pts/0 S+ 21:01 0:00 grep --color=auto nginx |
| |
| [root@proxy02 nginx-1.29.1] |
| |
| [root@proxy02 nginx-1.29.1] |
| root 5676 0.0 0.2 56960 4040 ? S 20:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5677 0.0 0.1 57404 1972 ? S 20:47 0:00 nginx: worker process |
| nginx 5678 0.0 0.1 57404 3480 ? S 20:47 0:00 nginx: worker process |
| root 5702 0.0 0.0 108096 616 pts/1 S+ 20:51 0:00 tail -f /var/log/nginx/error.log |
| root 5744 2.0 0.2 59436 4028 ? S 21:02 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5745 0.0 0.1 59880 1976 ? S 21:02 0:00 nginx: worker process |
| nginx 5746 0.0 0.1 59880 1976 ? S 21:02 0:00 nginx: worker process |
| root 5748 0.0 0.0 112812 976 pts/0 S+ 21:02 0:00 grep --color=auto nginx |
# 3.4 向旧的 master 发送 WINCH 信号
| |
| [root@proxy02 nginx-1.29.1] |
| [root@proxy02 nginx-1.29.1] |
| root 5676 0.0 0.2 56960 4040 ? S 20:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| root 5702 0.0 0.0 108096 616 pts/1 S+ 20:51 0:00 tail -f /var/log/nginx/error.log |
| root 5744 0.0 0.2 59436 4028 ? S 21:02 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5745 0.0 0.1 59880 1976 ? S 21:02 0:00 nginx: worker process |
| nginx 5746 0.0 0.1 59880 3500 ? S 21:02 0:00 nginx: worker process |
| root 5751 0.0 0.0 112812 976 pts/0 S+ 21:03 0:00 grep --color=auto nginx |
# 3.5 向旧的 master 发送 QUIT 信号
| |
| [root@proxy02 nginx-1.29.1] |
| [root@proxy02 nginx-1.29.1] |
| root 5702 0.0 0.0 108096 616 pts/1 S+ 20:51 0:00 tail -f /var/log/nginx/error.log |
| root 5744 0.0 0.2 59436 4028 ? S 21:02 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| nginx 5745 0.0 0.1 59880 1976 ? S 21:02 0:00 nginx: worker process |
| nginx 5746 0.0 0.1 59880 3500 ? S 21:02 0:00 nginx: worker process |
| root 5761 0.0 0.0 112812 980 pts/0 S+ 21:05 0:00 grep --color=auto nginx |