<!-- review: finished -->

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

# 缓存清除

允许清除缓存内容。

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

## 安装

要 [安装](https://cn.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>

## 单独缓存清除位置的配置示例

```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)
