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 1CentOS上安装Nginx服务器实现虚拟主机和域名重定向
CentOS 6.8 安装LNMP环境(Linux+Nginx+MySQL+PHP)
Linux下安装PHP环境并配置Nginx支持php-fpm模块