Nginx热升级

准备

这假设已经编译好了新版本的nginx的二进制文件。如下:

cd ~/nginx-1.14.2/objs/
ls -l nginx
-rwxr-xr-x. 1 root root 5789128 4月   3 16:12 nginx

注意:可以使用如下命令,查看以前nginx的编译参数:

# /var/nginx -V
...
configure arguments: --prefix=/var/nginx --with-http_ssl_module --with-http_realip_module

备份旧nginx

cp /var/nginx/sbin/nginx /var/nginx/sbin/nginx.old

覆盖旧nginx

cp -rf ~/nginx-1.14.2/objs/nginx /var/nginx/sbin/nginx

对比一下新旧nginx二进制文件:

cd /var/nginx/sbin/
 ls -l
总用量 11256
-rwxr-xr-x. 1 root root 5789128 4月   3 16:26 nginx
-rwxr-xr-x. 1 root root 5730664 4月   3 16:20 nginx.old

非root用户1024以下端口

setcap cap_net_bind_service=+eip /var/nginx/sbin/nginx

启用新nginx的master进程

查看旧的nginx进程情况:

ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15551  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15552  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15553  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15554  9412  0 16:52 ?        00:00:00 nginx: worker process
root     15556  9963  0 16:52 pts/1    00:00:00 grep --color=auto nginx

向旧nginx的master进程发送USR2信号,加载新的nginx二进制文件,启动新的nginx的master进程:

kill -USR2 9412
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15551  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15552  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15553  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15554  9412  0 16:52 ?        00:00:00 nginx: worker process
nginx    15574  9412  0 16:54 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15575 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15576 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15577 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15578 15574  0 16:54 ?        00:00:00 nginx: worker process
root     15580  9963  0 16:54 pts/1    00:00:00 grep --color=auto nginx

可以看到新起来的master进程是属于旧的master进程的子进程的,并且新旧的work进程都起来了。

关闭旧nginx的work进程

新的master进程起来的同时,新的work进程也启动了,这里需要优雅的退出旧work进程,即向旧的master进程发送WINCH信号退出旧的work进程:

kill -WINCH 9412
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15574  9412  0 16:54 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15575 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15576 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15577 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15578 15574  0 16:54 ?        00:00:00 nginx: worker process
root     15608  9963  0 16:57 pts/1    00:00:00 grep --color=auto nginx

到这里新的work进程就已经完全替换了旧的work进程。

收尾工作

完成上面的工作,其实,新的nginx已经在正常的工作了,如果运行一段时间,出现因为nginx升级导致的错误,就需要进行回滚操作;或者,运行一段时间,没有问题,nginx旧的master进程就可以彻底退出了。

回滚——还原旧nginx的work进程

回滚方式1

# 查看进程状况
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15574  9412  0 16:54 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15575 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15576 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15577 15574  0 16:54 ?        00:00:00 nginx: worker process
nginx    15578 15574  0 16:54 ?        00:00:00 nginx: worker process
root     15685  9963  0 17:01 pts/1    00:00:00 grep --color=auto nginx
# 发送USR1信号给旧的master进程,准备回滚
kill -USR1 9412
# 发送QUIT信号给新的master进程,彻底退出新的master进程
kill -QUIT 15574
# 查看进程状况
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15686  9412  0 17:01 ?        00:00:00 nginx: worker process
nginx    15687  9412  0 17:01 ?        00:00:00 nginx: worker process
nginx    15688  9412  0 17:01 ?        00:00:00 nginx: worker process
nginx    15689  9412  0 17:01 ?        00:00:00 nginx: worker process
root     15691  9963  0 17:01 pts/1    00:00:00 grep --color=auto nginx

回滚方式2

# 查看进程状况
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15720  9412  0 17:04 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15721 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15722 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15723 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15724 15720  0 17:04 ?        00:00:00 nginx: worker process
root     15728  9963  0 17:04 pts/1    00:00:00 grep --color=auto nginx
# 发送HUP信号给旧的master进程,准备回滚
kill -HUP 9412
# 查看进程状况
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15720  9412  0 17:04 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15721 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15722 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15723 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15724 15720  0 17:04 ?        00:00:00 nginx: worker process
nginx    15737  9412  0 17:05 ?        00:00:00 nginx: worker process
nginx    15738  9412  0 17:05 ?        00:00:00 nginx: worker process
nginx    15739  9412  0 17:05 ?        00:00:00 nginx: worker process
nginx    15740  9412  0 17:05 ?        00:00:00 nginx: worker process
root     15742  9963  0 17:05 pts/1    00:00:00 grep --color=auto nginx
# 发送QUIT信号给新的master进程,彻底退出新的master进程
kill -QUIT 15720
# 查看进程状况
ps -ef | grep nginx
nginx     9412     1  0 15:12 ?        00:00:00 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
nginx    15737  9412  0 17:05 ?        00:00:00 nginx: worker process
nginx    15738  9412  0 17:05 ?        00:00:00 nginx: worker process
nginx    15739  9412  0 17:05 ?        00:00:00 nginx: worker process
nginx    15740  9412  0 17:05 ?        00:00:00 nginx: worker process
root     15752  9963  0 17:06 pts/1    00:00:00 grep --color=auto nginx

升级成功——彻底退出旧nginx的master进程

kill -QUIT 9412

temp文件权限问题

ERR_INCOMPLETE_CHUNKED_ENCODING 200

err_content_length_mismatch

这里问题解决主要就是文件夹权限问题:

chown -R nginx proxy_temp

参考

转载于:https://my.oschina.net/fxtxz2/blog/3032029

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值