Caching is good for your WordPress blogs. In simplest terms, caching is a process whereby web pages/content are stored away for future use by the web server. For example, if user a visits your site and reads a particular page, the web server will fetch all data related to that page for user a, then store it away a copy for future use.

If user a or b comes back to that same page, instead of the webserver go to fetch the same page again, it returns with the stored copy of the page with all content related to it.. and that’s how caching works.

Using caching can help speed up WordPress or your other websites. This brief tutorial is going to show you how to use caching with Nginx web server.

Last week we showed you how to install WordPress on top of Nginx web server. If you followed that post and successfully installed Nginx, then continue with this to enable Nginx built-in caching with help of FastCGI module.

For more on FastCGI, FastCGI.

The first to do before you enable FastCGI on Nginx is to confirm Nginx web server is already functioning. Make sure Nginx is able to serve pages and is working as expected. If you can’t confirm Nginx is working, then go back to the previous post to configure Nginx.

After you’ve confirming Nginx is functioning OK, run the commands below to install FastCGI module, if you haven’t already done so.

sudo apt-get install php5-fpm

After installing FastCGI module, run the commands below to start it up.

sudo systemctl start php5-fpm

To enable it so that it automatically startup everytime your server starts, run the commands below.

sudo systemctl enable php5-fpm

Next, go and configure FastCGI module to enable Nginx caching. Since Nginx’s configuration files are saved in /etc/nginx, run the commands below to open Nginx main config file to edit.

sudo vi /etc/nginx/nginx.conf

Then add this block of code in the http { } block  (just above this line  include /etc/nginx/conf.d/*.conf;)

fastcgi_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=MYCACHE:15m inactive=60m;
fastcgi_cache_key “$scheme$request_method$host$request_uri”;
fastcgi_cache_use_stale error timeout invalid_header http_500;

Save the config file and close out.

The above code tells Nginx web serer to store cached content in /var/cache/nginx/cache folder for 15 mins. Then clears inactive content after 60 mins. These settings are just example can be tweaked to meet your needs.

Next, go to your Nginx factcgi php config file and add the lines below as shown.

Open Nginx default site on Ubuntu at /etc/nginx/snippets/fastcgi-php.conf

sudo vi /etc/nginx/snippets/fastcgi-php.conf

Then add the lines below

fastcgi_cache MYCACHE;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1h;
fastcgi_cache_min_uses 2;

Save the file and close out.

Before you restart Nginx web server to test, run the commands below to make sure Nginx configuration is correct.

nginx -t

If everything is configured correctly as shown above, you should get the lines below

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now check Nginx cache location to verify content are being cached.

One downside of caching WordPress content via FastCGI is the backend (admin areas) are also cached. Without a help of some plugins, it would be impossible to edit or work in the admin areas.

Install this plugin, https://wordpress.org/plugins/nginx-cache/ so that everytime changes occur in the backend, it clears out WordPress caches automatically.

The plugin can be downloaded from here.

Enjoy!