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

 
   
   
 
  1. wget https://openresty.org/download/openresty-1.15.8.3.tar.gz

  2. tar -xzvf openresty-1.15.8.3.tar.gz

2. 编译安装openresty

 
   
   
 
  1. cd /downloads/openresty-1.15.8.3

  2. ./configure --prefix=/usr/local/

  3. gmake

  4. gmake install

3. 平滑迁移Nginx

  • 检查nginx二进制文件和 nginx.pid的位置

 
   
   
 
  1. [root@test_192.168.1.83 /downloads/openresty-1.15.8.3]# cd /usr/local/nginx/sbin/

  2. [root@test_192.168.1.83 /usr/local/nginx/sbin]# ll

  3. total 20952

  4. -rwxr-xr-x 1 root root 16303050 Aug 11 20:28 nginx

  5. -rwxr-xr-x 1 root root 5147818 Aug 11 20:01 nginx.old

  6. [root@test_192.168.1.83 /usr/local/nginx/sbin]#

原来应该手动执行以下2步,但是openresty帮我们执行完了,这里就不用手动执行了

  • 备份旧的nginx文件

 
   
   
 
  1. cp nginx nginx.old

  • 复制新的nginx文件到/usr/local/nginx/sbin中

 
   
   
 
  1. cd /downloads/openresty-1.15.8.3/build/nginx-1.15.8/

  2. cp -rfp objs/nginx /usr/local/nginx/sbin/

  • 执行信号 kill-USR2 ${nginx_master_pid}平滑升级

 
   
   
 
  1. [root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx

  2. root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx

  3. www 30070 30069 0 20:27 ? 00:00:00 nginx: worker process

  4. www 30071 30069 0 20:27 ? 00:00:00 nginx: worker process

  5. www 30072 30069 0 20:27 ? 00:00:00 nginx: worker process

  6. www 30073 30069 0 20:27 ? 00:00:00 nginx: worker process

  7. www 30074 30069 0 20:27 ? 00:00:00 nginx: worker process

  8. www 30075 30069 0 20:27 ? 00:00:00 nginx: worker process

  9. www 30076 30069 0 20:27 ? 00:00:00 nginx: worker process

  10. www 30077 30069 0 20:27 ? 00:00:00 nginx: worker process

  11. root 30735 8315 0 20:29 pts/0 00:00:00 grep nginx

  12. [root@test_192.168.1.83 /usr/local/nginx]# kill -USR2 30069

可以看到,启动了一个新的nginx_master进程以及相应的worker进程。

 
   
   
 
  1. [root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx

  2. root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx

  3. www 30070 30069 0 20:27 ? 00:00:00 nginx: worker process

  4. www 30071 30069 0 20:27 ? 00:00:00 nginx: worker process

  5. www 30072 30069 0 20:27 ? 00:00:00 nginx: worker process

  6. www 30073 30069 0 20:27 ? 00:00:00 nginx: worker process

  7. www 30074 30069 0 20:27 ? 00:00:00 nginx: worker process

  8. www 30075 30069 0 20:27 ? 00:00:00 nginx: worker process

  9. www 30076 30069 0 20:27 ? 00:00:00 nginx: worker process

  10. www 30077 30069 0 20:27 ? 00:00:00 nginx: worker process

  11. root 30761 30069 1 20:29 ? 00:00:00 nginx: master process ./nginx

  12. www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process

  13. www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process

  14. www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process

  15. www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process

  16. www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process

  17. www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process

  18. www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process

  19. www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process

  20. root 30771 8315 0 20:29 pts/0 00:00:00 grep nginx

还可以看到,旧的 nginx.pid文件被修改为了 nginx.pid.oldbin,同时生成了一个新的 nginx.pid文件用来记录新的nginx master进程的pid。

 
   
   
 
  1. [root@test_192.168.1.83 /usr/local/nginx]# ll

  2. total 44

  3. drwx------ 2 www root 4096 Aug 11 15:29 client_body_temp

  4. drwxr-xr-x 3 root root 4096 Aug 4 21:42 conf

  5. drwx------ 12 www root 4096 Jun 20 2017 fastcgi_temp

  6. drwxr-xr-x 2 root root 4096 Nov 5 2015 html

  7. drwxr-xr-x 2 root root 4096 Jul 28 13:53 logs

  8. -rw-r--r-- 1 root root 6 Aug 11 20:29 nginx.pid

  9. -rw-r--r-- 1 root root 6 Aug 11 20:27 nginx.pid.oldbin

  10. drwx------ 12 www root 4096 Mar 28 2018 proxy_temp

  11. drwxr-xr-x 2 root root 4096 Aug 11 20:28 sbin

  12. drwx------ 2 www root 4096 Nov 5 2015 scgi_temp

  13. drwx------ 2 www root 4096 Nov 5 2015 uwsgi_temp

  • 执行 kill-WINCH ${nginx_master_pid}结束工作进程

 
   
   
 
  1. [root@test_192.168.1.83 /usr/local/nginx]# kill -WINCH 30069

  2. [root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx

  3. root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx

  4. root 30761 30069 0 20:29 ? 00:00:00 nginx: master process ./nginx

  5. www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process

  6. www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process

  7. www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process

  8. www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process

  9. www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process

  10. www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process

  11. www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process

  12. 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进程

 
   
   
 
  1. [root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx

  2. root 5185 8315 0 21:11 pts/0 00:00:00 grep nginx

  3. root 30069 1 0 20:27 ? 00:00:00 nginx: master process ./nginx

  4. root 30761 30069 0 20:29 ? 00:00:00 nginx: master process ./nginx

  5. www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process

  6. www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process

  7. www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process

  8. www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process

  9. www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process

  10. www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process

  11. www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process

  12. www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process

  13. [root@test_192.168.1.83 /usr/local/nginx]# kill -QUIT 30069

  14. [root@test_192.168.1.83 /usr/local/nginx]# ps -ef | grep nginx

  15. root 5212 8315 0 21:11 pts/0 00:00:00 grep nginx

  16. root 30761 1 0 20:29 ? 00:00:00 nginx: master process ./nginx

  17. www 30762 30761 0 20:29 ? 00:00:00 nginx: worker process

  18. www 30763 30761 0 20:29 ? 00:00:00 nginx: worker process

  19. www 30764 30761 0 20:29 ? 00:00:00 nginx: worker process

  20. www 30765 30761 0 20:29 ? 00:00:00 nginx: worker process

  21. www 30766 30761 0 20:29 ? 00:00:00 nginx: worker process

  22. www 30767 30761 0 20:29 ? 00:00:00 nginx: worker process

  23. www 30768 30761 0 20:29 ? 00:00:00 nginx: worker process

  24. www 30769 30761 0 20:29 ? 00:00:00 nginx: worker process

  25. [root@test_192.168.1.83 /usr/local/nginx]#

可以看到旧的master进程已经消失了,新的master进程父进程id变为了1

  • 查看nginx版本,验证是否升级成功

 
   
   
 
  1. [root@test_192.168.1.83 /usr/local/nginx/sbin]# ./nginx -v

  2. nginx version: openresty/1.15.8.3

  3. [root@test_192.168.1.83 /usr/local/nginx/sbin]#

可以看到已经升级至 nginx 1.15.8.3


以上是关于Nginx平滑升级至OpenResty的主要内容,如果未能解决你的问题,请参考以下文章

OpenResty 平滑升级

lamp架构nginx并发优化平滑升级与回滚

lamp架构nginx并发优化平滑升级与回滚

lamp架构nginx并发优化平滑升级与回滚

Linux----------nginx平滑升级

nginx平滑升级