平稳切换nginx版本
Nginx系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html
在说明如何稳定安全地升级、降级已经在运行中的nginx之前,需要先了解nginx支持的几种信号。以下几种是主进程可以接收的信号,注意worker进程也可以接收一些信号,但和主进程的信号处理机制有些不一样,且主进程支持的信号worker进程不一定支持。具体可见man nginx。
SIGINT, SIGTERM 立即杀掉nginx主(即所有进程)
SIGQUIT graceful stop主进程
SIGWINCH graceful stop所有的worker进程
SIGHUP reload配置文件,并使老的worker进程graceful stop
SIGUSR1 重新打开日志文件(Reopen log files)
SIGUSR2 在线切换nginx可执行程序(Upgrade the nginx executable on the fly)
graceful stop的行为是:(1)进程不再监听、接受新的请求;(2)进程继续处理正在处理的请求,但处理完成后销毁。
1. 升级
如果想对一个已运行的nginx实例进行版本升级,或者因为重新编译了一个版本而替换旧版本,可以考虑按照以下一系列过程来平稳、安全地升级。当然,如果直接停止服务不会产生多大影响,直接停掉再启动新版本nginx实例更方便简单。
- 1.将新版本的nginx命令路径替换掉旧的nginx命令。
通常,对于编译安装的nginx来说,采用软链接的方式比较便捷。例如旧版本的安装路径为/usr/local/nginx-1.12.0,为其建立一个软链接/usr/local/nginx,如果有新版本/usr/local/nginx-1.12.1,只需修改软链接/usr/local/nginx的指向目标为/usr/local/nginx-1.12.1即可。这样/usr/local/nginx/sbin/nginx就会随着软链接的指向改变而指向新nginx程序。
- 2.对旧nginx实例的主进程发送USR2信号。
kill -USR2 `cat /var/run/nginx/nginx.pid`
该信号提示nginx旧的主进程要升级,并执行新的nginx程序。例如步骤1中,旧的nginx主进程为/usr/local/nginx/sbin/nginx,但其指向的是/usr/local/nginx-1.12.0/sbin/nginx,发送该信号后仍将执行/usr/local/nginx/sbin/nginx,但此时因为软链接目标已改变,使得此时启动的nginx已经是/usr/local/nginx-1.12.1/sbin/nginx程序。
[root@xuexi ~]# ps aux | egrep '(ngin[x]|PI[D])'
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 103753 0.0 0.3 122720 7360 ? S 17:01 0:00 nginx: master process /usr/sbin/nginx
nginx 103754 0.0 0.1 125248 3520 ? S 17:01 0:00 nginx: worker process
nginx 103755 0.0 0.1 125248 3520 ? S 17:01 0:00 nginx: worker process
nginx 103756 0.0 0.1 125248 3520 ? S 17:01 0:00 nginx: worker process
nginx 103757 0.0 0.1 125248 3520 ? S 17:01 0:00 nginx: worker process
root 103919 0.0 0.3 122720 7364 ? S 17:44 0:00 nginx: master process /usr/sbin/nginx
nginx 103920 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
nginx 103921 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
nginx 103922 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
nginx 103923 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
此外,发送该信号后将会切换pid文件,旧的pid文件被重命名为nginx.pid.oldbin,记录的是旧的nginx主进程pid值,新的pid文件为nginx.pid,记录的是新启动的nginx的主进程pid值。
[root@xuexi ~]# ls /var/run/nginx*
/var/run/nginx.pid /var/run/nginx.pid.oldbin
- 3.graceful stop旧的主进程号。
kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`
向旧的主进程号发送QUIT信号,该信号将使得主进程以graceful的方式关闭。这将使得旧的主进程、旧的worker进程不再接受任何新请求,但却会把正在处理过程中的请求处理完毕,然后被销毁退出。
- 4.更稳妥的方式是先让worker进程graceful stop,在新版本的nginx实例运行一小段时间后如果正常工作,再graceful stop旧的主进程。
kill -WINCH `cat /var/run/nginx/nginx.pid.oldbin`
# a period of time goes, graceful stop old master nginx
kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`
在发送WINCH信号给旧的主进程后,旧的worker进程将逐渐退出,但旧的主进程却会保留不退出。
[root@xuexi ~]# ps aux | egrep '(ngin[x]|PI[D])'
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 103753 0.0 0.3 122720 7432 ? S 17:01 0:00 nginx: master process /usr/sbin/nginx
root 103919 0.0 0.3 122720 7364 ? S 17:44 0:00 nginx: master process /usr/sbin/nginx
nginx 103920 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
nginx 103921 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
nginx 103922 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
nginx 103923 0.0 0.1 125248 3524 ? S 17:44 0:00 nginx: worker process
如果发现新版本的nginx实例不满意,可以直接向旧主进程号发送HUP信号,这样旧的主进程就会重新读取配置文件并fork新的worker进程,再将新的主进程号杀掉(可以graceful stop),就可以还原为旧版本的nginx实例。
2.降级
上面第4步其实就是最安全的降级方式。即:
kill -HUP `cat /var/run/nginx/nginx.pid.oldbin`
kill -QUIT `cat /var/run/nginx/nginx.pid`
但如果旧的主进程号已经被杀掉了,目前只有新版本的nginx实例在运行,那么只需以升级的步骤进行降级即可。即:
kill -USR2 `cat /var/run/nginx/nginx.pid`
kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`
3.一键升级脚本
以下是升级的脚本。
#!/bin/sh
#
# Legacy action script for "service nginx upgrade"
# Source function library.
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/nginx ]; then
. /etc/sysconfig/nginx
fi
prefix=/usr/local/nginx
prog=nginx
nginx=$prefix/sbin/nginx
conffile=$prefix/conf/nginx.conf
pidfile=/var/run/nginx.pid
SLEEPSEC=${SLEEPSEC:-1}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS:-5}
oldbinpidfile=${pidfile}.oldbin
# 配置文件语法检查
${nginx} -t -c ${conffile} -q || return 6
echo -n $"Starting new master $prog: "
# 发送USR2信号升级nginx可执行程序
killproc -p ${pidfile} ${prog} -USR2
echo
for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
/bin/sleep $SLEEPSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
# graceful stop旧主nginx主进程
killproc -p ${oldbinpidfile} ${prog} -QUIT
echo
exit 0
fi
done
echo $"Upgrade failed!"
exit 1
相关文章
- Nginx教程(6) 动静分离架构
- linux环境中,openssl升级及openresty中nginx基于新版本openssl重新编译
- nginx 80端口跳转到443
- Nginx 笔记与总结(16)nginx 负载均衡
- Nginx 启用 OCSP Stapling的配置
- 我为什么要谈KeepAlive(文末增加nginx 负载tcp长连接保持 demo)
- Nginx反向代理2--配置文件配置
- Linux-019-Centos Shell 安装 Nginx 后启动时提示找不到Lua模块的libluajit-5.1.so.2文件,具体提示信息:./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
- nginx访问静态资源
- nginx听课随记杂记
- nginx服务器的负载均衡和动静分离(未完)
- windows下nginx的安装及使用方法入门
- Apache与Nginx的优缺点比较
- web cache server方案比较:varnish、squid、nginx
- Nginx配置文件nginx.conf中文详解
- 使用NGINX+LUA实现WAF功能 和nginx 防盗链
- 开发者论坛一周精粹(第十四期):CVE-2017-7529:Nginx敏感信息泄露
- FastDFS(1):Centos7 安装FastDFS+nginx module
- nginx基于域名的虚拟主机配置实战
- nginx学习笔记(7)Nginx如何处理一个请求---转载
- Nginx没有启动文件、nginx服务不支持chkconfig、nginx无法自启
- LVS/Nginx/HAProxy负载均衡器的对比分析
- 树莓派安装 Nginx + PHP7.0 + Pi Dashboard