Enable Super FastCGI Nginx Cache for WordPress on Ubuntu Cloud

Enable Nginx Cache on your LEMP stack to boost the website and blog loading speed with server side setup. The cache will be automatically created and start loading to boost site performance.

Enable the cache is one of the best technique to improve the site loading speed. But, most of the websites and bloggers use WordPress plugin. However, I personally prefer using server-side caching technique i.e. FastCGI Nginx Cache.

While using Plugin for WordPress cache works good, but it impacts on the overall server performance as it needs heavy PHP processing power. Instead, we can harness the Nginx web server to get better caching without executing the MySQL queries or utilize PHP processor.

In this article, we are going to learn how to set up and enable the FastCGI Nginx Cache on Ubuntu Cloud server to exponentially improve the WordPress site loading speed.

If you’re following us, this is the ninth article and video demo in our #CloudServer series. We previously posted about enabling browser cache and gzip compression in Nginx.

Let’s dive into creating the FastCGI Nginx Cache —

Create FastCGI-Cache configuration under /snippets/

Just like we created the browser cache and gzip compression in our previous article. We need to create a fastcgi-cache.conf file.

Creating FastCGI Cache Configuration for Nginx

Create and open the file under /etc/nginx/snippets/ and add the following rules:

# The key to use when saving cache files, which will run through the MD5 hashing algorithm.
fastcgi_cache_key "$scheme$request_method$host$request_uri";

# If an error occurs when communicating with FastCGI server, return cached content.
# Useful for serving cached content if the PHP process dies or timeouts.
fastcgi_cache_use_stale error timeout invalid_header http_500;

# Allow caching of requests which contain the following headers.
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

# Show the cache status in server responses.
add_header X-Cache $upstream_cache_status;

# Don't skip by default
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}

if ($query_string != "") {
set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/deploy/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*|/ideas.*|/wishlist.*") {
set $skip_cache 1;
}

# Don't use the cache for cookied logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|woocommerce_cart_hash|woocommerce_items_in_cart") {
set $skip_cache 1;
}

Nginx Cache FastCGI Configuration file

This will add the conditions when the cache should be processed, and also display a header response with X-Cache to understand if our Nginx Cache is HIT or MISS.

Saving FastCGI Cache in Ubuntu Cloud Server

Close the fastcgi-cache.conf file and head over to default nginx configuration file.

Include FastCGI-Cache rules in Nginx

Open the default nginx configuration file located under /etc/nginx/sites-available. You can open configuration file in nano editor.

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

Now, add the following line to include the fastcgi-cache.conf within server { .. } block that we created in previous step:

# Fastcgi cache rules
include snippets/fastcgi-cache.conf;

Include FastCGI Cache Snippets in Server Directive Block

This will help to include and follow the rules that we mentioned in fastcgi-cache configuration file.

Adding the FastCGI Nginx Cache path

Open default nginx configuration file and add the below line for setting up nginx fastcgi-cache path:

# This config file uses nginx fastcgi-cache
fastcgi_cache_path /var/www/demo/cache levels=1:2 keys_zone=restorebindemo:100m inactive=60m;

define FastCGI Cache Path before server block in Nginx

This will help Nginx to read and write the cache files into the path and serve when requested.

Define FastCGI Cache Rules

Next, within the location ~ \.php$ { ... } directive add the following FastCGI-cache rules in the same default:

# Skip cache based on rules in snippets/fastcgi-cache.conf.
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;

# Define memory zone for caching. Should match key_zone in fastcgi_cache_path above.
fastcgi_cache restorebindemo;

# Define caching time.
fastcgi_cache_valid 60m;
#increase timeouts
fastcgi_read_timeout 6000;
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
proxy_read_timeout 6000;
proxy_connect_timeout 6000;
proxy_send_timeout 6000;
send_timeout 6000;

FastCGI Cache Valid and Timeout Conditions

This will help nginx to keep the cache valid for 60 minutes and also set the timeout length. Now that we are done with adding and editing, save and close the default configuration file.

Restart and Test FastCGI Nginx Cache

We have made all the changes needed for Nginx Cache to work. Test Nginx configuration file changes with below command:

sudo nginx -t

And restart the web server to commit the changes to live server with below command.

sudo service nginx restart

Now we can test the Nginx Cache in our SSH to read the response header using below command. We need to execute the following command twice to verify whether FastCGI cache parameter X-Cache is MISS or HIT.

sudo curl -I https://demo.restorebin.com

Nginx Cache HIT & MISS

We can also check in the Chrome browser under developer options to verify the Nginx Cache.

Clear Nginx Cache using WordPress Plugin

We have successfully configured the Nginx cache and defined the cache valid for 60 mins. But, if we make any changes on the post or UI that won’t reflect immediately. Hence, we need a cache buster for cache purging.

We can use a super easy WordPress plugin called Nginx Cache by Till Kr├╝ss. Follow the below steps:

  1. Login to WordPress Dashboard
  2. Navigate to Add New under Plugins menu
  3. Search and Install the Nginx Cache plugin
  4. Activate the Plugin
    Nginx Cache Plugin by Till Kruss
  5. On the Nginx Cache setting page, enter the fastcgi_cache_path that we’ve mentioned in Nginx Configuration file — /var/www/demo/cache
  6. Enable the checkbox for Automatically flush the cache when content changes
  7. That’s it!

Nginx cache Purge WordPress Plugin

You can also find a new menu Nginx added on the WordPress Admin bar to Purge Cache at ease to clear the Nginx Cache manually.

Watch Nginx Cache Setup Guide

Watch the video tutorial on how to enable the FastCGI Nginx Cache at server side. I’ve recorded exact the step by step actions that are written in this article.

Enable FastCGI Nginx Cache for WordPress on Ubuntu Cloud

I hope you liked the video, please subscribe to our channel on YouTube.

What’s next in #CloudServer series?

Now that we have learned about how to enable the Nginx Cache at server side. Next, we are going to touch base upon enabling the Redis Cache for WordPress Database Caching.

If you've any thoughts on Enable Super FastCGI Nginx Cache for WordPress on Ubuntu Cloud, then feel free to drop in below comment box. Also, please subscribe to our restoreBin YouTube channel for amazing videos tips. Cheers!

Share

Leave a Reply

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