nginx+php-fpm有时候会提示Nginx 502 Bad Gateway,并且不会自动恢复
不过可以写个脚本检测服务状态,发现异常,自动重启。
自动重启脚本:
$url = ‘http://www.duzhi.net’;
$cmd = ‘/etc/init.d/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();
}
}
?>
把以上代码保存成nginx502.php,然后添加定时任务,每分钟执行一次。
定时任务代码如下
*/1 * * * * /usr/bin/php /root/crontab/nginx502.php
小插曲:如果执行后php报错:Warning: shell_exec() has been disabled for security reasons in ……
说明你的php不支持shell_exec(),只需要修改php.ini打开这个功能即可。