nginx + php-fpm 502临时解决办法

因为经常弄的VPS跑着跑着502,但是还没有找到具体的原因,所以无奈采用了一个比较简陋的临时解决方案,具体办法就是定时检测网站的状态码,如果发现是502,就重启php-fpm。

具体我这里有两个代码,一个是PHP实现,一个是Shell实现,都是我在VPS上用过的,域名都给隐藏成了www.xxx.com , 最后把这脚本任意放到地方,写到Crontab里去就OK了。

这个只能临时的解决一下问题,具体什么导致的502,还得细致的分析一下避免才行。

[php]

#!/usr/bin/php
<?
$url = ‘http://www.xxx.com';
$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);
$echo502=’echo $(date +"%Y-%m-%d %H:%M:%S") Find 502 Bad Gateway! >> /root/502/$(date +"%Y%m%d").log’;
shell_exec($echo502);
exit();
}
}
?>
[/php]

[shell]

#!/bin/bash

MY_URL="http://www.xxx.com"
RESULT=curl -I $MY_URL|grep &quot;HTTP/1.1 502&quot;

if [ -n "$RESULT" ]; then

#reboot
/etc/init.d/php-fpm restart

fi
[/shell]