This article mainly introduced PHP script monitoring Nginx 502 error and automatically restart PHP-FPM, this article directly give the implementation code, and then put into cron implementation, need friends can refer to the
Recently the server appears Nginx 502 Bad Gateway, if the computer next to good, if the middle of the night or out, how to do?
It doesn't matter, write a script to detect service status, find an exception, and restart automatically.
Automatic Restart Script:
The code is as follows:
$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 ();
}
}
The principle is to use Curl to obtain HTTP headers, found 502 status code to perform the restart PHP-FPM command.
URL and cmd changed to their own according to the actual situation. and put it in the crontab for one minute.
The code is as follows:
*/1 * * * */usr/bin/php/root/crontab/nginx502.php
Done!