fbpx

Server Setup

ngx_cache_purge: closing the gap with Varnish

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

The ngx_cache_purge provides you with the ability to clear specific NGINX cache entries easily.
This is a feature present in NGINX Plus, but now you can easily add it to your open source NGINX using our packaged module.

How to install ngx_cache_purge in CentOS/RHEL or Amazon Linux

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install nginx-module-cache-purge

To enable this module, add the following to /etc/nginx/nginx.conf and reload nginx:

load_module modules/ngx_http_cache_purge_module.so;

The module allows you to allow purging cache of NGINX for the following types of caches:

  • FastCGI
  • Proxy
  • scgi
  • uwcgi

To benefit from it, your web app should have the ability to send HTTP PURGE requests to your website, e.g. WordPress user may want to use Proxy Cache Purge plugin together with the fastcgi_cache_purge directive below.

Status

This module is production-ready.

Configuration directives (same location syntax)

fastcgi_cache_purge

  • syntax: fastcgi_cache_purge on|off|<method> [purge_all] [from all|<ip> [.. <ip>]]
  • default: none
  • context: http, server, location

Allow purging of selected pages from FastCGI‘s cache.

proxy_cache_purge

  • syntax: proxy_cache_purge on|off|<method> [purge_all] [from all|<ip> [.. <ip>]]
  • default: none
  • context: http, server, location

Allow purging of selected pages from proxy‘s cache.

scgi_cache_purge

  • syntax: scgi_cache_purge on|off|<method> [purge_all] [from all|<ip> [.. <ip>]]
  • default: none
  • context: http, server, location

Allow purging of selected pages from SCGI‘s cache.

uwsgi_cache_purge

  • syntax: uwsgi_cache_purge on|off|<method> [purge_all] [from all|<ip> [.. <ip>]]
  • default: none
  • context: http, server, location

Allow purging of selected pages from uWSGI‘s cache.

Configuration directives (separate location syntax)

fastcgi_cache_purge

  • syntax: fastcgi_cache_purge zone_name key
  • default: none
  • context: location

Sets area and key used for purging selected pages from FastCGI‘s cache.

proxy_cache_purge

  • syntax: proxy_cache_purge zone_name key
  • default: none
  • context: location

Sets area and key used for purging selected pages from proxy‘s cache.

scgi_cache_purge

  • syntax: scgi_cache_purge zone_name key
  • default: none
  • context: location

Sets area and key used for purging selected pages from SCGI‘s cache.

uwsgi_cache_purge

  • syntax: uwsgi_cache_purge zone_name key
  • default: none
  • context: location

Sets area and key used for purging selected pages from uWSGI‘s cache.

Configuration directives (Optional)

cache_purge_response_type

  • syntax: cache_purge_response_type html|json|xml|text
  • default: html
  • context: http, server, location

Sets a response type of purging result.

Partial Keys

Sometimes it’s not possible to pass the exact key cache to purge a page. For example; when the content of a cookie or the params are part of the key.
You can specify a partial key adding an asterisk at the end of the URL.

curl -X PURGE /page*

The asterisk must be the last character of the key, so you must put the $uri variable at the end.

Sample configuration (same location syntax)

http {
    proxy_cache_path  /tmp/cache  keys_zone=tmpcache:10m;

    server {
        location / {
            proxy_pass         http://127.0.0.1:8000;
            proxy_cache        tmpcache;
            proxy_cache_key    $uri$is_args$args;
            proxy_cache_purge  PURGE from 127.0.0.1;
        }
    }
}

Sample configuration (same location syntax – purge all cached files)

http {
    proxy_cache_path  /tmp/cache  keys_zone=tmpcache:10m;

    server {
        location / {
            proxy_pass         http://127.0.0.1:8000;
            proxy_cache        tmpcache;
            proxy_cache_key    $uri$is_args$args;
            proxy_cache_purge  PURGE purge_all from 127.0.0.1;
        }
    }
}

Sample configuration (separate location syntax)

http {
    proxy_cache_path  /tmp/cache  keys_zone=tmpcache:10m;

    server {
        location / {
            proxy_pass         http://127.0.0.1:8000;
            proxy_cache        tmpcache;
            proxy_cache_key    $uri$is_args$args;
        }

        location ~ /purge(/.*) {
            allow              127.0.0.1;
            deny               all;
            proxy_cache_purge  tmpcache $1$is_args$args;
        }
    }
}

Sample configuration (Optional)

http {
    proxy_cache_path  /tmp/cache  keys_zone=tmpcache:10m;

    cache_purge_response_type text;

    server {

        cache_purge_response_type json;

        location / { #json
            proxy_pass         http://127.0.0.1:8000;
            proxy_cache        tmpcache;
            proxy_cache_key    $uri$is_args$args;
        }

        location ~ /purge(/.*) { #xml
            allow              127.0.0.1;
            deny               all;
            proxy_cache_purge  tmpcache $1$is_args$args;
            cache_purge_response_type xml;
        }

        location ~ /purge2(/.*) { # json
            allow              127.0.0.1;
            deny               all;
            proxy_cache_purge  tmpcache $1$is_args$args;
        }
    }

    server {

        location / { #text
            proxy_pass         http://127.0.0.1:8000;
            proxy_cache        tmpcache;
            proxy_cache_key    $uri$is_args$args;
        }

        location ~ /purge(/.*) { #text
            allow              127.0.0.1;
            deny               all;
            proxy_cache_purge  tmpcache $1$is_args$args;
        }

        location ~ /purge2(/.*) { #html
            allow              127.0.0.1;
            deny               all;
            proxy_cache_purge  tmpcache $1$is_args$args;
            cache_purge_response_type html;
        }
    }
}

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.