redis_cache with conditional purging

easyengine (ee) note: If you are using easyengine, you can accomplish everything in this article using command:

ee site create example.com --wpredis

Nginx does not have built-in support for redis_cache.  We need four Nginx modules: srcache-nginx-moduleredis2-nginx-moduleHttpRedisModule and set-misc-nginx-module to get everything working on Nginx.

We will have to rely on third party nginx module. Without this module, you won’t be able to cache with  Redis.

Prerequisites

You will need redis server installed to cache your site with redis.

If you have installed Nginx from our repository you will have these modules already available with Nginx . You can test with following command:

nginx -V 2>&1 | grep 'srcache-nginx-module\|redis2-nginx-module\|HttpRedisModule\|set-misc-nginx-module' -o

If you see  following output, then you have it.

srcache-nginx-module
HttpRedisModule
redis2-nginx-module
set-misc-nginx-module

Otherwise, if you are on Ubuntu or Debian with Nginx default installation, you can use following commands  to install nginx with necessary modules from our nginx repo.

Reinstall Nginx with modules that support Redis cache

# For Ubuntu 16.04
sudo sh -c "echo 'deb http://download.opensuse.org/repositories/home:/rtCamp:/EasyEngine/xUbuntu_16.04/ /' >> /etc/apt/sources.list.d/nginx.list"
sudo apt-get update
sudo apt-get install nginx-custom nginx-ee

# For Debian 8
echo 'deb http://download.opensuse.org/repositories/home:/rtCamp:/EasyEngine/Debian_8.0/ /' >> /etc/apt/sources.list.d/nginx.list 
apt-get update
apt-get install nginx-custom nginx-ee

For other versions of Ubuntu and Debian, install packages as per instructed here.

Install Nginx Helper Plugin

Above steps ensures that you have necessary modules with Nginx, so that Nginx can store and access cache from redis. Nginx cannot automatically find out which page to purge and when to purge?

So install Nginx helper plugin from WordPress plugin repository and activate it. Apart from other features, it provides cache purging options. Just activate it.

Nginx Helper  Settings

1. Enable Cache Purge 

1

2. Cacheing Method (Redis Cache)

   Provide redis server hostname , port and  prefix you have used for cacheing.

2

3. Select Cache Purging conditions

3

Save your settings.

Nginx Config

Now let’s make changes to /etc/nginx/sites-available/example.com file so that it looks like one below

server {
    server_name example.com www.example.com;

    access_log   /var/log/nginx/example.com.access.log;
    error_log    /var/log/nginx/example.com.error.log;

    root /var/www/example.com/htdocs;
    index index.php;

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

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

    location /redis-fetch {
        internal  ;
        set  $redis_key $args;
        redis_pass  127.0.0.1:6379;
    }

    location /redis-store {
        internal  ;
        set_unescape_uri $key $arg_key ;
        redis2_query  set $key $echo_request_body;
        redis2_query expire $key 14400;
        redis2_pass  127.0.0.1:6379;
    } 

    location ~ \.php$ {
        
        set $key "nginx-cache:$scheme$request_method$host$request_uri";
        try_files $uri =404;

        srcache_fetch_skip $skip_cache;
        srcache_store_skip $skip_cache;

        srcache_response_cache_control off;

        set_escape_uri $escaped_key $key;

        srcache_fetch GET /redis-fetch $key;
        srcache_store PUT /redis-store key=$escaped_key;

        more_set_headers 'X-Cache $srcache_fetch_status';
        more_set_headers 'X-Cache-2 $srcache_store_status';

        include fastcgi_params;
        fastcgi_pass php;
    } 

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\. { deny  all; access_log off; log_not_found off; }
}

Don’t Forget

Always test your Nginx configuration and reload it. All changes to Nginx config must be followed by these commands

nginx -t && service nginx reload

Links: Nginx-Helper Plugin | EasyEngine

1 response to “redis_cache with conditional purging”