博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
平滑升级Nginx的Shell脚本
阅读量:4180 次
发布时间:2019-05-26

本文共 2794 字,大约阅读时间需要 9 分钟。

        Nginx平滑升级说明,来自《Nginx Http Server - Updating Nginx gracefully》,这里不进行翻译了,原文如下:

        There are many situations where you need to replace the Nginx binary, for example, when you compile a new version and wish to put it in production or simply after having enabled new modules and rebuilt the application. What most administrators would do in this situation is stop the server, copy the new binary over the old one, and start Nginx again. While this is not considered to be a problem for most websites, there may be some cases where uptime is critical and connection losses should be avoided at all costs. Fortunately, Nginx embeds a mechanism allowing you to switch binaries with uninterrupted uptime - zero percent request loss is guaranteed if you follow these steps carefully:

       1. Replace the old Nginx binary( by default, /usr/local/nginx/sbin/nginx) with the new one.

       2. Find the pid of the Nginx master process, for example, with  ps x | grep nginx | grep master or by looking at the value found in the pid file.

       3. Send a USR2 signal to the master process - kill -USR2 ***, replacing ***  with the pid found in step 2. This will initiate the upgrade by renaming

the old  .pid file and running the new binary.

       4. Send a WINCH (28) signal to the old master process—kill –WINCH ***, replacing ***  with the pid found in step 2. This will engage a graceful

shutdown of the old worker processes.

       5. Make sure that all the old worker processes are terminated, and then send a QUIT signal to the old master process—kill –QUIT ***, replacing  ***  with the pid found in step 2.

Shell脚本如下:

#!/bin/sh# 进行nginx软件平滑升级的shell脚本# 提示更新nginx文件echo -e '首先更新nginx文件,然后[回车]继续'read yes#if [ "$yes" != "Y" -o "$yes" != "y"]; then#    echo "未成功升级nginx"#    exit 100#fi# 记录master process的pidold_master_pid=`ps -ef | grep 'nginx: master process' | grep -v ' grep ' | awk '{print $2}'`echo 'master pid:' $old_master_pid# 记录worker process的pidold_workers_pid=`ps -ef | grep 'nginx: worker process' | grep -v ' grep ' | awk '{print $2}'`echo 'worker process pid:' $old_workers_pid# 向nginx发送USR2信号,重命名.pid为.pid.old,并启动新的nginxkill -USR2 $old_master_pid# 向nginx发送WINCH信号,平滑处理已链接请求kill -WINCH $old_master_pid# 等待旧的worker process是否全部退出isOldWorkerProcessAllExit='yes'while truedo    cur_workers_pid=`ps -ef | grep 'nginx: worker process' | grep -v ' grep ' | awk '{print $2}'`    # 判断旧的worker process进程是否退出,如果全部退出,则跳出循环    for pid in $old_workers_pid    do        subIndex=`awk 'BEGIN{print match("'"$cur_workers_pid"'","'"$pid"'")}'`        if [ "$subIndex" -ne "0" ];  then            isOldWorkerProcessAllExit="no"            break             fi        done    if [ "$isOldWorkerProcessAllExit" == "yes" ]; then        break        fi        sleep 1done# 向旧的master process发送QUIT信号,使其退出kill -QUIT $old_master_pidecho "nginx升级成功!"exit 0

转载地址:http://xkwoi.baihongyu.com/

你可能感兴趣的文章
aop详解和基于spring-aop xml的简单编程
查看>>
【java小程序】zookeeper监听并自动更新
查看>>
软碟通系统U盘制作教程
查看>>
【java多线程编程】三种多线程的实现方式
查看>>
【java多线程】线程常用操作方法总结
查看>>
【java多线程】线程的同步与死锁
查看>>
【java多线程】生产者与消费者多线程同步案例
查看>>
【java多线程】守护线程、线程停止、volatile的深入了解
查看>>
StringBuffer对String类型的操作
查看>>
CharSequence详情介绍
查看>>
Runtime获取系统资源信息类
查看>>
了解Schema约束
查看>>
一个简单的webService接口发布与实现
查看>>
案例:天气预报的接口调用
查看>>
使用cxf编写基于spring的web service
查看>>
ajax、Jquery、HttpUrlConnectin请求web service
查看>>
通过注解修改wsdl文档
查看>>
初步了解JVM
查看>>
深入JVM内核----原理、诊断与优化
查看>>
jvm运行机制
查看>>