Cache Purge#
Позволяет очищать содержимое кэша.
Установка#
Для установки модуля используйте один из следующих пакетов:
Angie:
angie-module-cache-purge;Angie PRO:
angie-pro-module-cache-purge.
Загрузка модуля#
Подключение модуля в контексте main{}:
load_module modules/ngx_http_cache_purge_module.so;
Пример конфигурации#
log_format test_cache '[$time_local] "$request" '
'$status $body_bytes_sent rt="$request_time" '
'ucs="$upstream_cache_status" us="$upstream_status" '
'ubr=$upstream_bytes_received';
proxy_cache_path /var/cache/angie/cache keys_zone=cache_zone:10m;
server {
listen 80 default_server;
location / {
access_log /var/log/angie/cache-access.log test_cache;
proxy_pass http://127.0.0.1:8080;
proxy_cache cache_zone;
proxy_cache_valid 10m;
proxy_cache_key $uri$is_args$args;
proxy_cache_purge PURGE from 127.0.0.1;
}
}
Подготовка к демонстрации#
Запрос файла на сервер:
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
Проверка кэша (MISS — файл отсутствует в кэше):
[05/Mar/2025:17:44:24 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.001" ucs="MISS" us="200" ubr=752
Повторный запрос (HIT — файл отдается из кэша):
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
[05/Mar/2025:17:46:02 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.000" ucs="HIT" us="-" ubr=-
Очистка кэша#
$ curl -X PURGE http://127.0.0.1/storage/*
<html><head><title>Successful purge</title></head><body bgcolor="white"><center><h1>Successful purge</h1><p>Key : /storage/*</p></center></body></html>
Запрос файла после очистки кэша (MISS — файл загружен заново):
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
[05/Mar/2025:17:52:05 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.002" ucs="MISS" us="200" ubr=752
Пример конфигурации для отдельного location очистки кэша#
proxy_cache_path /var/cache/angie/cache keys_zone=cache_zone:10m;
map $uri $wpurgeuri {
"~/purge(?<wpurge>/.*)" $wpurge;
default $uri;
}
server {
listen 80 default_server;
location / {
access_log /var/log/angie/cache-access.log test_cache;
proxy_pass http://127.0.0.1:8080;
proxy_cache cache_zone;
proxy_cache_valid 10m;
proxy_cache_key $uri$is_args$args;
}
location /purge {
allow 127.0.0.1;
deny all;
proxy_cache_purge cache_zone $wpurgeuri$is_args$args;
}
}
Запрос на очистку кэша в этом случае:
$ curl -X PURGE http://127.0.0.1/purge/storage/*
Дополнительная информация#
Версия: 3.0.2
Полное описание директив и исходный код доступны по ссылке: nginx-modules/ngx_cache_purge