<!-- review: finished -->

<a id="external-cache-purge"></a>

# Cache Purge

Позволяет очищать содержимое кэша.

<a id="installation-5"></a>

## Установка

Для [установки](https://angie.software//angie/docs/installation/index.md#install-packages) модуля используйте один из
следующих пакетов:

- Angie: `angie-module-cache-purge`;
- Angie PRO: `angie-pro-module-cache-purge`.

<a id="loading-the-module-4"></a>

## Загрузка модуля

Подключение модуля в контексте `main{}`:

```nginx
load_module modules/ngx_http_cache_purge_module.so;
```

<a id="configuration-example-81"></a>

## Пример конфигурации

```nginx
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;
    }
}
```

<a id="preparing-for-demonstration-1"></a>

## Подготовка к демонстрации

Запрос файла на сервер:

```console
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
```

Проверка кэша (MISS — файл отсутствует в кэше):

```console
[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 — файл отдается из кэша):

```console
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
```

```console
[05/Mar/2025:17:46:02 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.000" ucs="HIT" us="-" ubr=-
```

<a id="clearing-the-cache"></a>

## Очистка кэша

```console
$ 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 — файл загружен заново):

```console
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
```

```console
[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
```

<a id="configuration-example-for-a-separate-cache-clearing-location"></a>

## Пример конфигурации для отдельного location очистки кэша

```nginx
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;
    }
}
```

Запрос на очистку кэша в этом случае:

```console
$ curl -X PURGE http://127.0.0.1/purge/storage/*
```

<a id="additional-information-5"></a>

## Дополнительная информация

Полное описание директив и исходный код доступны по ссылке:
[https://github.com/nginx-modules/ngx_cache_purge](https://github.com/nginx-modules/ngx_cache_purge)
