Dealing PHP-FPM Crash & Nginx 502 Bad Gateway Error with Perl FastCGI

Continuing from my previous post, “Dealing PHP-FPM Crash & Nginx 502 Bad Gateway Error with Shell Scripts“…

<center> fail to restart PHP-FPM.</center> fail to restart PHP-FPM.

So I worked on further trying to improvise this. Then an idea struck me, why not catch the error and try to solve it before giving up? With this, I came up with additional method. By introducing a simple Perl script into the chain, it would be able to catch the 502 Bad Gateway errors beforehand and try to fix it, thus making it transparent to visitors. Although there’ll be a slight delay (PHP5-FPM restarting and re-caching the Opcache/APCU), hopefully I won’t have to lose a hit. Right now I have both Shell and Perl scripts running hand in hand. I wouldn’t say this is hundred percent 502 Bad Gateway errors proof but it does help lessen the issue.

<center> custom themed.</center> custom themed.

If somehow the restart process failed (usually multiple site hits per second), visitors will be presented with a modified HTTP 502 Bad Gateway error page. To be able to achieve this setup, firstly we’ll need to have Perl FastCGI enabled in Nginx which you’ll obviously need root access. I’m not covering this here as there’s already an official page on how to have it running, Setting up Perl FastCGI with Nginx. But if you have installed Nginx from my latest WDMyCloud WebHosting Mods at the WD Forum here, [APP] WebHosting for firmware V4+, Perl FastCGI is already enabled.

Disclaimer: As I’m frequently updating the original guides and installers here on, I will not be responsible for any brick issues if you were to follow my obsolete guides copied elsewhere. Thus subscribe to this post to get latest updates. Modifying any part of a device may void its warranty.

After Perl FastCGI has been enabled, continue reading the script’s notes on how to get it installed. You could also easily blend in the final error with your site’s theme. Just point the “$template” variable to a plain html file on your server root, then add a string “” anywhere you wish in the body content.

A point to take note is that having PHP-FPM always restarting on 502 errors does take a hit on your server’s performance, especially if Nginx is mainly serving static or cached files. This script should be used as a last resort.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see