Skip to content

热升级部署

  1. 将旧的NGINX文件替换成新的NGINX文件
  2. 向master进程发送USR2信号
  3. master进程修改pid文件,加后缀.oldbin
  4. master进程用新nginx文件启动新master进程
  5. 向旧的master进程发WINCH信号,旧的worker子进程退出
  6. 回滚情形:向旧master发送HUP,向新的master发送QUIT
sh
# 将旧的NGINX文件替换成新的NGINX文件
cp nginx nginx.bak

-rwxr-xr-x 1 root root 9.3M Feb 11 02:37 nginx
-rwxr-xr-x 1 root root 9.3M Feb 11 03:05 nginx.bak

#查看进程
ps -ef | grep nginx

root     21930     1  0 03:03 ?        00:00:00 nginx: master process sbin/nginx
nginx    21931 21930  0 03:03 ?        00:00:00 nginx: worker process

# 向master进程发送USR2信号
kill -s SIGUSR2 <旧pid 21930>

root     21930     1  0 03:03 ?        00:00:00 nginx: master process sbin/nginx
nginx    21931 21930  0 03:03 ?        00:00:00 nginx: worker process
root     22089 21930  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
nginx    22095 22089  0 03:05 ?        00:00:00 nginx: worker process

# master进程修改pid文件,加后缀.oldbin
ll  /usr/local/nginx/logs

total 12K
-rw-r--r-- 1 root root  0 Feb 11 02:38 access.log
-rw-r--r-- 1 root root 76 Feb 11 03:05 error.log
-rw-r--r-- 1 root root  6 Feb 11 03:05 nginx.pid
-rw-r--r-- 1 root root  6 Feb 11 03:03 nginx.pid.oldbin

# 停止旧的NGINX子进程,旧主进程仍存在
kill -s SIGWINCH <旧pid 21930>

root     21930     1  0 03:03 ?        00:00:00 nginx: master process sbin/nginx
root     22089 21930  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
nginx    22095 22089  0 03:05 ?        00:00:00 nginx: worker process

#测试新进程,没问题,停止旧的主进程
kill -s SIGQUIT <旧pid 21930>

root     22089     1  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
nginx    22095 22089  0 03:05 ?        00:00:00 nginx: worker process

# master进程用新nginx文件启动新master进程
ll  /usr/local/nginx/logs

total 12K
-rw-r--r-- 1 root root  0 Feb 11 02:38 access.log
-rw-r--r-- 1 root root 76 Feb 11 03:05 error.log
-rw-r--r-- 1 root root  6 Feb 11 03:05 nginx.pid

回滚情形

  • 向旧的master进程发WINCH信号,旧的worker子进程退出
  • 向旧master发送HUP,向新的master发送QUIT
sh
# 新旧进程并存状态
kill -s SIGUSR2 <旧pid 22089>

root     22089     1  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
nginx    22095 22089  0 03:05 ?        00:00:00 nginx: worker process
root     22242 22089  0 03:21 ?        00:00:00 nginx: master process sbin/nginx
nginx    22248 22242  0 03:21 ?        00:00:00 nginx: worker process

# master进程修改pid文件,加后缀.oldbin
ll  /usr/local/nginx/logs

total 12K
-rw-r--r-- 1 root root  0 Feb 11 02:38 access.log
-rw-r--r-- 1 root root 76 Feb 11 03:05 error.log
-rw-r--r-- 1 root root  6 Feb 11 03:05 nginx.pid
-rw-r--r-- 1 root root  6 Feb 11 03:03 nginx.pid.oldbin

# 停止旧的NGINX子进程,旧主进程仍存在
kill -s SIGWINCH <旧pid 22089>

root     22089     1  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
root     22242 22089  0 03:21 ?        00:00:00 nginx: master process sbin/nginx
nginx    22248 22242  0 03:21 ?        00:00:00 nginx: worker process

#验证新版本不符合预期, 重新启动旧的进程
kill -s SIGHUP <旧pid>

root     22089     1  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
root     22242 22089  0 03:21 ?        00:00:00 nginx: master process sbin/nginx
nginx    22248 22242  0 03:21 ?        00:00:00 nginx: worker process
nginx    22476 22089  0 03:37 ?        00:00:00 nginx: worker process

#退出新的主进程及子进程
kill -s SIGQUIT <旧pid 21930>

root     22089     1  0 03:05 ?        00:00:00 nginx: master process sbin/nginx
nginx    22476 22089  0 03:37 ?        00:00:00 nginx: worker process