nginx+php-fpm出现502错误的终极解决方案
利用Nignx+php-fpm架构服务器的linux运维工程师们肯定都碰到过502 Bad Gateway错误的情况,出现502 Bad Gateway错误是由于php-fpm进程挂掉了,当然,当出现502 Bad Gateway错误的同时,我们也不得不说下504 timeout错误了。504错误是因为Php-fpm进程全部被占用,没有php进程来处理更多的访问引起的。这边有一个当网站出现502 Bad Gateway错误时自动去杀掉然后重启php-fpm的脚本。
#!/bin/sh
#502重启php
if [ -e /var/lock/subsys/502 ]
then
killall -9 curl 2>/dev/null
killall -9 php-fpm 2>/dev/null
/usr/local/webserver/php/sbin/php-fpm >/dev/null
echo "[ `date +'%h%d %T'` ] PHP-FPM died with no response, all processes restarted">>/data/logs/php.log
else
touch /var/lock/subsys/502
if [ `curl --connect-timeout 5 -I http://www.arezone.cn/ 2>/dev/null | grep '502 Bad Gateway' -c` != '0' ]
then
killall -9 php-fpm 2>/dev/null
/usr/local/webserver/php/sbin/php-fpm >/dev/null
echo "[ `date +'%h%d %T'` ] PHP-FPM died with 502 bad gateway, all processes restarted">>/data/logs/php.log
fi
rm -f /var/lock/subsys/502
fi
当出现502或者没有回应时写入日志文件
[ Mar24 09:14:01 ] PHP-FPM died with no response, all processes restarted
[ Mar26 08:39:01 ] PHP-FPM died with no response, all processes restarted
[ Mar26 08:41:00 ] PHP-FPM died with 502 bad gateway, all processes restarted
[ Mar26 08:44:01 ] PHP-FPM died with no response, all processes restarted
[ Mar26 09:37:02 ] PHP-FPM died with 502 bad gateway, all processes restarted