Skip to content

selective-cache-purge: NGINX Selective Cache Purge module

Installation

You can install this module in any RHEL-based distribution, including, but not limited to:

  • RedHat Enterprise Linux 7, 8, 9 and 10
  • CentOS 7, 8, 9
  • AlmaLinux 8, 9
  • Rocky Linux 8, 9
  • Amazon Linux 2 and Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-selective-cache-purge
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-selective-cache-purge

Enable the module by adding the following at the top of /etc/nginx/nginx.conf:

load_module modules/ngx_selective_cache_purge_module.so;

This document describes nginx-module-selective-cache-purge v0.8.0 released on Mar 26 2018.


A module to purge cache by GLOB patterns. The supported patterns are the same as supported by Redis.

Configuration

An example:

pid         logs/nginx.pid;
error_log   logs/nginx-main_error.log debug;

# Development Mode
# master_process      off;
# daemon              off;
worker_processes    1;
worker_rlimit_core  500M;
working_directory /tmp;
debug_points abort;

events {
    worker_connections  1024;
    #use                 kqueue; # MacOS
    use                 epoll; # Linux
}

http {
    default_type    application/octet-stream;

    access_log      logs/nginx-http_access.log;
    error_log       logs/nginx-http_error.log;

    proxy_cache_path /tmp/cache_zone levels=1:2 keys_zone=zone:10m inactive=10d max_size=100m;
    proxy_cache_path /tmp/cache_other_zone levels=1:2 keys_zone=other_zone:1m inactive=1d max_size=10m;

    #selective_cache_purge_redis_unix_socket "/tmp/redis.sock";
    #
    # or
    #
    #selective_cache_purge_redis_host "localhost";
    #selective_cache_purge_redis_port 6379;

    selective_cache_purge_redis_database 1;

    server {
        listen          8080;
        server_name     localhost;

        # purging by prefix
        location ~ /purge(.*) {
            selective_cache_purge_query "$1*";
        }

        location / {
            proxy_pass http://localhost:8081;

            proxy_cache zone;
            proxy_cache_key "$uri";
            proxy_cache_valid 200 1m;
        }
    }

    server {
        listen          8090;
        server_name     localhost;

        # purging by extension
        location ~ /purge/.*(\..*)$ {
            #purge by extension
            selective_cache_purge_query "*$1";
        }

        location / {
            proxy_pass http://localhost:8081;

            proxy_cache other_zone;
            proxy_cache_key "$uri";
            proxy_cache_valid 200 1m;
        }
    }

    server {
        listen          8081;
        server_name     localhost;

        location / {
            return 200 "requested url: $uri\n";
        }
    }
}

Installation instructions

This module requires: - Redis 2.8 or newer. Install it with your favourite package manager - apt-get, yum, brew - or download Redis and compile it. - hiredis 0.11.0. Install it with your favourite package manager - apt-get, yum, brew - or download hiredis and compile it. - redis_nginx_adapter library

Download Nginx Stable source and uncompress it. You must then run ./configure with --add-module pointing to this project as usual, referencing the up-to-date hiredis/redis_nginx_adapter lib and include if they are not on your default lib and include folders. Something in the lines of:

$ ./configure \
    --with-ld-opt='-L/usr/lib/ ' \
    --with-cc-opt='-I/usr/include/hiredis/ ' \
    --add-module=/path/to/nginx-selective-cache-purge-module
$ make
$ make install

Running tests

This project uses nginx_test_helper on the test suite. So, after you've installed the module, you can just install the necessary gems:

$ bundle install --gemfile=test/Gemfile

And run rspec pointing to where your Nginx binary is (default: /usr/local/nginx/sbin/nginx):

$ NGINX_EXEC=/path/to/nginx rspec test/

Changelog

This is still a work in progress. Be the change. And take a look on the Changelog.

GitHub

You may find additional configuration tips and documentation for this module in the GitHub repository for nginx-module-selective-cache-purge.