,

Enable FastCGI Caching on Nginx for WordPress

The article provides a detailed guide on enabling FastCGI caching with Nginx on Ubuntu Linux to significantly improve the performance of PHP-based applications. It involves installing the PHP FastCGI module, updating the Nginx main config file and website server block, and optionally integrating specific code for WordPress websites to exclude backend portal, sitemap, and other…

This article describes steps to enable FastCGI caching with Nginx on Ubuntu Linux.

If you are running a WordPress website, using FastCGI caching to improve performance should be considered.

When you combine Nginx and FastCGI modules, you will significantly improve your PHP-based applications. FastCGI module caches dynamic PHP content that is served through the Nginx backend.

When dynamic PHP content is cached, repeated requests for the same content are quickly returned from the cache store instead of compiling all the dynamic data that make up the page each time a request is made.

Install PHP-FPM

As described above, combining Nginx and FastCGI modules will significantly improve your PHP-based applications. FastCGI module caches dynamic PHP content that is served through the Nginx backend.

To begin, install the PHP FastCGI module by executing the commands below.

sudo apt update
sudo apt install php-fpm

This article assumes you have Nginx installed and working. If you haven’t installed Nginx yet, read the following post to learn how to do so.

How to install Nginx on Ubuntu Linux

Set up FastCGI Nginx directive.

Nginx configuration files are stored in the [/etc/nginx] directory on Ubuntu Linux. Open the Nginx main configuration file [nginx.conf].

Run the commands below to open the Nginx configuration file.

sudo nano /etc/nginx/nginx.conf

After opening the file, copy the content below and paste it at the end. This section defines the path for the Nginx FastCGI cache and sets up the cache zone.

## Nginx FastCGI Cache
fastcgi_cache_path /var/cache/nginx/fastcgi_temp/cache levels=1:2 keys_zone=wpcache:100m inactive=60m;
fastcgi_cache_key $scheme$request_method$host$request_uri;
fastcgi_cache_lock on;
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_pass_header Set-Cookie:Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

Save and exit.

Set up WordPress server block

After configuring the Nginx FastCGI cache, open the default site configuration file and add a section to reference the cache settings created above.

On Ubuntu Linux, website server blocks are usually stored in the directory [/etc/nginx/sites-available].

Run the commands below to open the default server block file.

sudo nano /etc/nginx/sites-available/default

Next, copy and paste the content below into the file, usually found under the PHP section.

    location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache wpcache;
include fastcgi_params;
}

PHP should use the FastCGI module and enable caching if configured correctly.

As you are operating a WordPress website, it’s important to add the code below to your server block file just before the [location ~ .php$] section.

This will ensure that caching is bypassed for the backend portal, sitemap, and other files that should not be cached.

set $skip_cache 0;
# POST requests and url's with a query string should always skip cache
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache url's containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}

Save your settings, and you are done.

Conclusion:

  • Enabling FastCGI caching in Nginx can significantly boost the performance of your WordPress site.
  • Proper configuration helps reduce server load by serving cached content for repeated requests.
  • Utilize the provided configuration steps to correctly set up FastCGI caching and the server block for optimal performance.
  • Be mindful of caching exceptions for admin areas, logged-in users, and dynamic content to maintain functionality.
  • Regularly monitor your cache settings and performance to ensure continued efficiency.

Richard Avatar

Comments

Leave a Reply

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