cache-purge: NGINX Cache Purge module
Debian/Ubuntu installation
These docs apply to the APT package nginx-module-cache-purge provided by the GetPageSpeed Extras repository.
- Configure the APT repository as described in APT repository setup.
- Install the module:
sudo apt-get update
sudo apt-get install nginx-module-cache-purge
Show suites and architectures
| Distro | Suite | Component | Architectures |
|----------|-------------------|-------------|-----------------|
| debian | bookworm | main | amd64, arm64 |
| debian | bookworm-mainline | main | amd64, arm64 |
| debian | trixie | main | amd64, arm64 |
| debian | trixie-mainline | main | amd64, arm64 |
| ubuntu | focal | main | amd64, arm64 |
| ubuntu | focal-mainline | main | amd64, arm64 |
| ubuntu | jammy | main | amd64, arm64 |
| ubuntu | jammy-mainline | main | amd64, arm64 |
| ubuntu | noble | main | amd64, arm64 |
| ubuntu | noble-mainline | main | amd64, arm64 |
ngx_cache_purge is nginx module which adds ability to purge content from
FastCGI, proxy, SCGI and uWSGI caches. A purge operation removes the
content with the same cache key as the purge request has.
Sponsors
Work on the original patch was fully funded by yo.se.
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 192.168.0.0/8;
}
}
}
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 tmpcache;
proxy_cache_key "$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 tmpcache;
proxy_cache_key "$1$is_args$args";
cache_purge_response_type xml;
}
location ~ /purge2(/.*) { # json
allow 127.0.0.1;
deny all;
proxy_cache tmpcache;
proxy_cache_key "$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 tmpcache;
proxy_cache_key "$1$is_args$args";
}
location ~ /purge2(/.*) { #html
allow 127.0.0.1;
deny all;
proxy_cache tmpcache;
proxy_cache_key "$1$is_args$args";
cache_purge_response_type html;
}
}
}
Solve problems
- Enabling
gzip_varycan lead to different results when clearing, when enabling it, you may have problems clearing the cache. For reliable operation, you can disablegzip_varyinside the location #20.
Testing
ngx_cache_purge comes with complete test suite based on Test::Nginx.
You can test it by running:
$ prove
See also
- ngx_slowfs_cache.
- http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#purger
- http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_purge
- https://github.com/wandenberg/nginx-selective-cache-purge-module
- https://github.com/wandenberg/nginx-sorted-querystring-module
- https://github.com/ledgetech/ledge
- Faking Surrogate Cache-Keys for Nginx Plus (gist)
- Delete NGINX cached md5 items with a PURGE with wildcard support