Google Cloud

FastCGI Cache with Nginx for WordPress on Ubuntu 18.04 LTS

Would you like to improve your WordPress performance? Enabling FastCGI and configuring it correctly for your blog or your Woocommerce store will greatly increase your WordPress Performance.

This brief tutorial will help you setup FastCGI on Google Cloud Ubuntu 18.04

When using Nginx with FastCGI module you can increase the performance of your web application or WordPress websites, because FastCGI caches the dynamic content served by Nginx.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Bluehost★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites

Nginx FastCGI Configurations

The first setup requires a few changes in your nginx.conf file to generate the key names and add a header for the responses, so you can easily determine whether the page is cached or not. Go ahead and edit the file and add the below directives below the GZIP section

sudo nano /etc/nginx/nginx.conf
##
# Cache Settings
##
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header Fastcgi-Cache $upstream_cache_status;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

Hit Ctrl+X followed by Y to save your Nginx configuration.

Now you can set up the FastCGI cache path and instruct Nginx not to cache certain pages like wp-admin, if user logged in and etc. Edit your website configuration file and add these

sudo nano /etc/nginx/sites-available/yourdomainname.com

Add this to the top of your configurations. Don’t forget to update the path and domain name with yours

fastcgi_cache_path /home/username/yourdomainname.com/cache levels=1:2 keys_zone=yyourdomainname.com:100m inactive=60m;

Now instruct Nginx not to cache the admin pages, Woocommerce dynamic pages, and others.

Add the below cache exceptions below the root path inside your server block

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;
}

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

# Don't cache URIs containing the following segments
if ($request_uri ~* "/wp-admin/|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
     set $skip_cache 1;
}
if ($request_uri ~* "/(cart|checkout|my-account)/*$") {
     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;
}

Now inside the php location block location ~ \.php$ { add these

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache yourdomainname.com;
fastcgi_cache_valid 60m;

Hit Ctrl+X followed by Y to save the configurations and restart Nginx

sudo nginx -t

Check for the Nginx configuration success message and proceed to restart Nginx

sudo service nginx restart

Now if you visit your website and check the headers, you can see a new header with these possible values

  1. HIT – Page cached
  2. MISS – Page not cached (refreshing should cause a HIT or BYPASS)
  3. BYPASS – Page cached but not served (admin screens or when logged in or Woocommerce account pages)
  4. STALE – Page is served from cache that is stored in cache directory.

Configure WordPress to purge the cache

Finally, install a plugin to purge the cache when WordPress content changes. Install the Nginx Cache plugin.

Once installed navigate to Tools >> Nginx and define the cache path you defined earlier and enable the purge cache option and save the settings

Now your WordPress is configured to use Nginx FastCGI cache.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.