PHP script monitors Nginx502 errors and automatically restarts php-fpm, nginxphp-fpm_PHP tutorial

Source: Internet
Author: User
The PHP script monitors Nginx502 errors and automatically restarts php-fpm and nginxphp-fpm. The PHP script monitors Nginx502 errors and automatically restarts php-fpm. nginxphp-fpm recently occurs on the server Nginx502BadGateway from time to time. if it is good to be on the computer, if it goes out in the middle of the night, PHP scripts monitor Nginx 502 errors and automatically restart php-fpm and nginxphp-fpm

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!

Listen 502 error and automatically restart php-fpm, nginxphp-fpm recent server appears Nginx 502 Bad Gateway from time to time, if it is good beside the computer, if it is midnight or go out ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.