如何只清理Nginx指定URL的缓存?
美国、香港服务器
如何只清理Nginx指定URL的缓存?
11-17 来源:
一 、核心配置(http 块)
http {
# 缓存目录,加 purger=on 支持通配清除
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mycache:10m max_size=1g inactive=60m purger=on;
map $request_method $purge_method {
PURGE 1;
default 0;
}
}
二、站点 server 块配置
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_cache mycache;
# 重点:缓存key必须和purge时一致
proxy_cache_key "$scheme$host$request_uri";
proxy_cache_valid 200 10m;
# 开启清除,只允许本机/内网执行PURGE(安全!)
proxy_cache_purge $purge_method;
add_header X-Cache-Status $upstream_cache_status;
}
}
安全提醒:**绝对不要公网开放 PURGE**,上面 map 方式配合防火墙,只本地 curl 调用。
三、清理指定 URL 命令(直接复制)
# 清理单个URL:https://example.com/abc.html
curl -X PURGE https://example.com/abc.html
# 清理带参数URL:https://example.com/list?page=1
curl -X PURGE "https://example.com/list?page=1"
- 返回 `204 No Content` ✅ 清除成功
- 返回 `404 Not Found` ⚠️ 该 URL 无缓存
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快