Issue
Drupal's 404 response is very resource intensive as it causes Drupal to fully bootstrap.
Resolution
Enabling Fast 404 in Drupal core
Drupal core has an option to serve a simple static HTML error response to a 404 status code. Enabling this setting can decrease wasted bandwidth and server load.
The instructions differ for Drupal 7 and Drupal 8. Follow the instructions that correspond to the Drupal version in use in your application.
Drupal 8
- Edit your website's settings.php file
- Find and uncomment (remove the #) the following lines:
# $config['system.performance']['fast_404']['exclude_paths'] = '/\/(?:styles)|(?:system\/files)\//'; # $config['system.performance']['fast_404']['paths'] = '/\.(?:txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i'; # $config['system.performance']['fast_404']['html'] = '<!DOCTYPE html><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>';
- Save the settings.php file
- Commit the changed code to your repository
Drupal 7
- Edit your website's
settings.php
file - Find and uncomment (remove the #) on the following line:
# drupal_fast_404();
- Save the
settings.php
file - Commit the changed code to your repository
Using the Fast 404 module
Drupal core's fast 404 option avoids expensive page rendering without the need for an additional module. However, the Fast 404 module improves on this core functionality with more options to reduce load caused by bad requests.
The instructions differ for Drupal 7 and Drupal 8. Follow the instructions that correspond to the Drupal version in use in your application.
Drupal 8
- Add the module to your
composer.json
file. To do so, open a command prompt window, go to the top level in your Drupal 8 directory (the directory that contains thecomposer.json
file) and run these commands:composer config repositories.drupal composer https://packages.drupal.org/8 composer require drupal/fast_404
- Place the code at the bottom of the
README.TXT
file into yoursettings.php
file - If the module was not installed in
/modules
, modify theinclude_once
path to match the install location - Commit the module and modified
settings.php
file to your repository - Enable the module on the modules page
Drupal 7
- Add the module to your standard modules location (usually
sites/all/modules
) - Place the code at the bottom of the
README.TXT
file into yoursettings.php
file - If the module was not installed in
sites/all/modules
, modify the include_once path to match the install location - Commit the module and modified
settings.php
file to your repository - Enable the module on the modules page
To maximize the effectiveness of the Fast 404 module it may be necessary to familiarize yourself with each configuration setting found within the code block of the README.TXT
file to determine whether it fits your needs.
Cause
By default, Drupal fully bootstraps for every 404 Not Found. This can be very expensive resource intensive; not just for missing pages, but any page with a bad link or a missing files can generate several 404s with one page request which can multiply the amount of resources needed to serve a single page.
Ideally, it would be best to eliminate all 404 requests; however, there are times when this is not a tenable option, such as developer resource constraints or a traffic spike. Thankfully, there are several methods to eliminate a fully bootstrapped response to a 404 Not Found request.