主页 > 运维运营 > Nginx >
发布时间:2015-12-29 作者:网络 阅读:323次

最近服务器时不时出现Nginx 502 Bad Gateway,如果在电脑旁边还好,要是半夜或者出去了,怎么办?
没关系,写个脚本检测服务状态,发现异常,自动重启。
自动重启脚本:

<?php
$url = 'http://blog.rebill.info';
$cmd = '/usr/local/php/sbin/php-fpm restart';
 
for($i = 0; $i < 5; $i ++){
        $exec = "curl  connect-timeout 3 -I $url 2>/dev/null";
        $res = shell_exec($exec);
 
        if(stripos($res, '502 Bad Gateway') !== false){
                shell_exec($cmd);
                exit();
        }
}


原理就是用curl获取HTTP头,发现502状态码就执行重启php-fpm的命令。
url和cmd根据实际情况改成自己的。然后放到crontab里一分钟执行一次。


*/1 * * * * /usr/bin/php /root/crontab/nginx502.php


大功告成!

关键字词: