This article mainly introduces how to monitor Nginx502 errors with PHP scripts and automatically restart php-fpm. This article provides the implementation code and then puts it in cron for execution. For more information, see
This article mainly introduces the PHP script to monitor Nginx 502 errors and automatically restart php-fpm. This article provides the implementation code and then puts it in cron for execution. For more information, see
Recently, Nginx 502 Bad Gateway appears on the server from time to time. If it is okay on the computer, what should I do if it goes out in the middle of the night?
It doesn't matter. Write a script to check the service status. If an exception is found, it will be restarted automatically.
Automatic Restart script:
The Code is 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, '2014, 502 Bad gateway ')! = False ){
Shell_exec ($ cmd );
Exit ();
}
}
The principle is to use curl to obtain the HTTP header and execute the command to restart php-fpm when the 502 status code is found.
The url and cmd are changed to your own according to the actual situation. Then run the command once a minute in crontab.
The Code is as follows:
*/1 *****/usr/bin/php/root/crontab/nginx502.php
Success!