VirtualminWeb DevelopmentWordPress

Install Cache Enabler for WordPress on Virtualmin-Nginx

Cache Enabler is a lightweight caching plugin for WordPress. If your WordPress website is on a Nginx web server, you’ll need rewrite rules for Cache Enabler to work optimally. These Nginx rewrite rules will work on WordPress single site and WordPress multisite as well as WordPress single site on a sub-directory with a little tweak.

I’m using Cache Enabler plugin on this blog at the moment of writing. I had used WP Super Cache for years. I still think WP Super cache is the best caching plugin for WordPress. But installing WooCommerce forced me to move away from WP Super cache as two plugins were not compatible on Nginx environment. Currently I don’t have anything against Cache Enabler plugin. It’s simple to set up and as good as WP Super Cache.

I’m writing this tutorial for WordPress hosted on Virtualmin-Nginx servers. So I’m going to assume that you have followed my tutorials to build your server or hired me to do it for you.

Let’s start by login in to Virtualmin control panel as root user. Once logged in, go to Webmin > Servers > Nginx Webserver and then click Edit Configuration Files. Select configuration file for your WordPress blog from the drop-down menu. It should look like /etc/nginx/sites-available/domain.com. Scroll down and find following code within the file,

location / {
                try_files $uri $uri/ /index.php?$args;
        }

Now delete those tree lines and paste the following lot,

    set $cache_uri $request_uri;

    # bypass cache if POST requests or URLs with a query string
    if ($request_method = POST) {
        set $cache_uri 'nullcache';
    }
    if ($query_string != "") {
        set $cache_uri 'nullcache';
    }

    # bypass cache if URLs containing the following strings
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $cache_uri 'nullcache';
    }

    # bypass cache if the cookies containing the following strings
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'nullcache';
    }

    # custom sub directory e.g. /blog
    set $custom_subdir '';

    # default html file
    set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index.html';

    # webp html file
    if ($http_accept ~* "image/webp") {
        set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index-webp.html';
    }

    location / {
        gzip_static on; # this directive is not required but recommended
        try_files $cache_enabler_uri $uri $uri/ $custom_subdir/index.php?$args;
    }

Save file and then click Apply Nginx Configurations. Now you can install Cache Enabler plugin from WordPress admin area. Feel free to play with plugin settings. Although it has very little configuration options, it gets the job done.

Cache Enabler for Sub-directory

If your WordPress blog is on a sub-directory, you must make a little change to the above rewrite rules. Look for the following line,

set $custom_subdir '';

You need to include your sub-directory in this line. If your WordPress blog is on example.com/blog, you need to change the code like below,

set $custom_subdir '/blog';

I hope you get the idea. Save file and apply Nginx configurations. Please leave a comment if you’re having trouble setting up Cache Enabler plugin. I’ll get back to you as soon as I can.

Tags

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Related Articles

One Comment

  1. Hello,

    thanks for the post – i just have one question, what is the benefit of editing the /location part – as i saw not much difference when i just installed / enables cache-enabler.

    Thanks!

Leave a Reply

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

Close