DNS and nginx server setup causes slow server and 502 response DNS and nginx server setup causes slow server and 502 response nginx nginx

DNS and nginx server setup causes slow server and 502 response


Heres whats happening.

It says in your error log server reached pm.max_children setting (15), consider raising it

So the max.children limit of 15 means PHP-FPM will stop launching processes once an app has 15 processes running, and any more requests for processes which come in will be queued until one of the previous processes ends.

You are using a php script to generate a 404 page, you then load a page with a load of broken links, your Nginx try files directive ends with a php script:

try_files $uri $uri/ /index.php?$args;

From the Nginx docs that means:

If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

So for every broken link you just added an extra php process to the queue. If you count your 502 errors in the log you'll see there are 15. Because Nginx looks for 15 /index.php?$args which it can't find so tries to display a 404 which guess what? Is generated in php and now everything is broken.

15 processes which cant return 404 because the process limit has been reached and they each need another process to generate a 404 page, so until they time out no more processes for you.

The whole idea of serving a 404 page this way is crazy anyway. It's a static page, you should be serving it from Nginx because web servers are really really good at delivering static content fast, passing it to php, which in turn requests it from your own server again makes absolutely no sense.

Download your custom page to a file:

curl -o /var/www/vuyk.eu/webroot/404.html https://test.vuyk.eu/404-page-not-found

Now add an error page directive in your Nginx conf:

error_page 404 /404.html;

and now you have Nginx serving a custom error page without changing the client url and absolutely no load on your server.


It's seems that images you try to load are unavailable and requests are passed to PHP where 404 page is generated. Your custom 404 page is fetching resources via http

echo file_get_contents('https://test.vuyk.eu/404-page-not-found'); 

If this fetch is slow your script might execute for very long time which might lead to timeouts. Also this might result in requests piling up and draining server resources.

Try to replace this fetch with something faster, you can try to read/include 404 page data directly from filesystem.


There are two broad possibilities as to why this is slow on the new server.

  1. Problems with Webserver / PHP
  2. Problems with DNS

To troubleshoot, enter the command line on your server and try to fetch a missing file using wget or cUrl. If you get a response as fast as you expect, then you most likely have an issue with your Webserver / PHP. If it is also slow, then the issue is with the DNS setup on your new server.

In any case, it appears that using file_get_contents for external URLs can lead to funky results. (Yes, The files are on your server but as you have a full url, it is treated like an external url).

So instead of ...

echo file_get_contents('https://test.vuyk.eu/404-page-not-found'); 

use

echo file_get_contents('/server/path/to/404-page-not-found');

If you can't do this because 404-page-not-found is not a physical file and has to be run through Joomla to be generated, then why not use cUrl instead? This is specifically for 'external' files.

function curlFile($url) {  $ch = curl_init();  curl_setopt($ch, CURLOPT_HEADER, 0);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  curl_setopt($ch, CURLOPT_URL, $url);  $ret = curl_exec($ch);  curl_close($ch);  return $ret;}echo curlFile('https://test.vuyk.eu/404-page-not-found');

Note that if you found a DNS issue, you will need to resolve that notwithstanding.