Nginx平滑升级至OpenResty
Posted DevOps探索之旅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx平滑升级至OpenResty相关的知识,希望对你有一定的参考价值。
nginx 平滑升级
nginx路径:/usr/local/nginx
升级前:nginx 1.4.4
升级后:openresty 1.15.8.3
升级步骤
1. 下载openresty
wget https://openresty.org/download/openresty-1.15.8.3.tar.gz
tar -xzvf openresty-1.15.8.3.tar.gz
2. 编译安装openresty
cd /downloads/openresty-1.15.8.3
./configure --prefix=/usr/local/
gmake
gmake install
3. 平滑迁移Nginx
检查nginx二进制文件和
nginx.pid
的位置
[root@test_192.168.1.83 /downloads/openresty-1.15.8.3]# cd /usr/local/nginx/sbin/
[root@test_192.168.1.83 /usr/local/nginx/sbin]# ll
total 20952
-rwxr-xr-x 1 root root 16303050 Aug 11 20:28 nginx
-rwxr-xr-x 1 root root 5147818 Aug 11 20:01 nginx.old
[root@test_192.168.1.83 /usr/local/nginx/sbin]#
原来应该手动执行以下2步,但是openresty帮我们执行完了,这里就不用手动执行了
备份旧的nginx文件
cp nginx nginx.old
复制新的nginx文件到/usr/local/nginx/sbin中
cd /downloads/openresty-1.15.8.3/build/nginx-1.15.8/
cp -rfp objs/nginx /usr/local/nginx/sbin/
执行信号
kill-USR2 ${nginx_master_pid}
平滑升级
[root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx
root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx
www 30070 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30071 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30072 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30073 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30074 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30075 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30076 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30077 30069 0 20:27 ? 00:00:00 nginx: worker process
root 30735 8315 0 20:29 pts/0 00:00:00 grep nginx
[root@test_192.168.1.83 /usr/local/nginx]# kill -USR2 30069
可以看到,启动了一个新的nginx_master进程以及相应的worker进程。
[root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx
root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx
www 30070 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30071 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30072 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30073 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30074 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30075 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30076 30069 0 20:27 ? 00:00:00 nginx: worker process
www 30077 30069 0 20:27 ? 00:00:00 nginx: worker process
root 30761 30069 1 20:29 ? 00:00:00 nginx: master process ./nginx
www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process
root 30771 8315 0 20:29 pts/0 00:00:00 grep nginx
还可以看到,旧的 nginx.pid
文件被修改为了 nginx.pid.oldbin
,同时生成了一个新的 nginx.pid
文件用来记录新的nginx master进程的pid。
[root@test_192.168.1.83 /usr/local/nginx]# ll
total 44
drwx------ 2 www root 4096 Aug 11 15:29 client_body_temp
drwxr-xr-x 3 root root 4096 Aug 4 21:42 conf
drwx------ 12 www root 4096 Jun 20 2017 fastcgi_temp
drwxr-xr-x 2 root root 4096 Nov 5 2015 html
drwxr-xr-x 2 root root 4096 Jul 28 13:53 logs
-rw-r--r-- 1 root root 6 Aug 11 20:29 nginx.pid
-rw-r--r-- 1 root root 6 Aug 11 20:27 nginx.pid.oldbin
drwx------ 12 www root 4096 Mar 28 2018 proxy_temp
drwxr-xr-x 2 root root 4096 Aug 11 20:28 sbin
drwx------ 2 www root 4096 Nov 5 2015 scgi_temp
drwx------ 2 www root 4096 Nov 5 2015 uwsgi_temp
执行
kill-WINCH ${nginx_master_pid}
结束工作进程
[root@test_192.168.1.83 /usr/local/nginx]# kill -WINCH 30069
[root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx
root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx
root 30761 30069 0 20:29 ? 00:00:00 nginx: master process ./nginx
www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process
可以看到,旧的worker进程已经优雅结束了(不再接收新的请求,处理完当前请求后自动结束,或者在等待了maxrequeststimeout之后强行结束)
此时旧的master进程id还在,可以通过 kill-HUP ${nginx_master_pid}
重新拉起旧的nginx
如果不想回滚,可以执行 kill-QUIT ${nginx_master_pid}
优雅结束master进程
[root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx
root 5185 8315 0 21:11 pts/0 00:00:00 grep nginx
root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx
root 30761 30069 0 20:29 ? 00:00:00 nginx: master process ./nginx
www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process
[root@test_192.168.1.83 /usr/local/nginx]# kill -QUIT 30069
[root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx
root 5212 8315 0 21:11 pts/0 00:00:00 grep nginx
root 30761 1 0 20:29 ? 00:00:00 nginx: master process ./nginx
www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process
www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process
[root@test_192.168.1.83 /usr/local/nginx]#
可以看到旧的master进程已经消失了,新的master进程父进程id变为了1
查看nginx版本,验证是否升级成功
[root@test_192.168.1.83 /usr/local/nginx/sbin]# ./nginx -v
nginx version: openresty/1.15.8.3
[root@test_192.168.1.83 /usr/local/nginx/sbin]#
可以看到已经升级至 nginx 1.15.8.3
以上是关于Nginx平滑升级至OpenResty的主要内容,如果未能解决你的问题,请参考以下文章