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:
Copy Code code as follows:
<?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 ();
}
}
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.
Copy Code code as follows:
*/1 * * * */usr/bin/php/root/crontab/nginx502.php
Done!