Nginx的proxy_cache模块是服务器运维中提升后端响应速度的高效方案。通过缓存后端应用返回内容,Nginx能直接从磁盘或内存读取数据返回客户端,避免每次请求都打到上游服务器。在内容更新频率不高的场景下,合理配置缓存能把后端QPS降低60%以上,整体响应延迟下降3倍以上。
proxy_cache核心指令解析
proxy_cache的工作流程:客户端请求到达Nginx后检查缓存键是否存在,命中则直接返回缓存内容,未命中则转发到上游服务器,上游返回后Nginx写入缓存。核心配置指令包括proxy_cache_path、proxy_cache_key、proxy_cache_valid。
缓存路径与内存区配置
在Nginx配置文件的http块中定义缓存路径:
http {
proxy_cache_path /var/cache/nginx levels=1:2
keys_zone=api_cache:100m max_size=10g
inactive=60m use_temp_path=off;
# levels=1:2 表示两级目录结构,避免单目录文件过多
# keys_zone=api_cache:100m 分配100MB共享内存存储缓存键
# max_size=10g 缓存磁盘最大占用10GB
# inactive=60m 60分钟未被访问则自动清理
}
keys_zone的内存大小决定能缓存多少个缓存键。每个缓存键约160字节,100MB共享内存可存储约65万条缓存记录。如果后端接口数量超过这个规模,需要按比例增大keys_zone。
Server块中的缓存策略配置
定义好缓存路径后,在server或location块中启用缓存:
server {
listen 80;
server_name api.example.com;
location /api/ {
proxy_pass http://backend_upstream;
proxy_cache api_cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid 500 0s;
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
add_header X-Cache-Status $upstream_cache_status;
}
}
$upstream_cache_status变量返回MISS、HIT、EXPIRED、STALE等状态,方便调试。MISS表示缓存未命中,HIT表示命中,EXPIRED表示缓存过期,STALE表示返回了过期缓存(配合proxy_cache_use_stale使用)。
缓存清理与自动刷新机制
Nginx默认没有缓存清除功能。两种实现方案。方案一:使用proxy_cache_bypass和proxy_no_cache,通过请求参数控制是否走缓存:
location /api/ {
proxy_cache api_cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_bypass $arg_nocache;
proxy_no_cache $arg_nocache;
proxy_pass http://backend_upstream;
}
# 请求 http://api.example.com/api/data?nocache=1 会直接打到后端
方案二:使用nginx-cache-purge模块(需编译安装),实现精确路径清除:
location ~ /purge(/.*) {
allow 10.0.0.0/8;
allow 127.0.0.1;
deny all;
proxy_cache_purge api_cache "$scheme$request_method$host$1";
}
# 访问 http://api.example.com/purge/api/data 即可清除对应缓存
缓存击穿与雪崩防护
缓存击穿指热点key过期瞬间大量请求穿透到后端。proxy_cache_lock开启后,同一个缓存键同时只有一个请求转发到上游,其余请求等待缓存写入。proxy_cache_lock_timeout设置等待超时,超时后直接转发。
# 缓存击穿防护
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
proxy_cache_lock_age 5s;
# 缓存雪崩防护:使用过期缓存兜底
proxy_cache_use_stale error timeout updating
http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_use_stale在后端不可用时返回过期缓存,保证服务可用性。proxy_cache_background_update开启后台异步更新,前台先返回过期缓存,后台同时刷新。
性能验证与监控
部署完成后用ab或wrk验证效果:
# 无缓存基准测试
ab -n 1000 -c 50 http://api.example.com/api/data
# 开启缓存后测试
ab -n 10000 -c 100 http://api.example.com/api/data
# 观察X-Cache-Status头
curl -I http://api.example.com/api/data
# 第一次: X-Cache-Status: MISS
# 第二次: X-Cache-Status: HIT
缓存命中率监控通过Nginx访问日志统计:
# 日志格式中添加缓存状态
log_format cache '$remote_addr - $upstream_cache_status [$time_local] '
'"$request" $status $body_bytes_sent';
access_log /var/log/nginx/api_cache.log cache;
# 统计命中率
awk '{print $4}' /var/log/nginx/api_cache.log | sort | uniq -c | sort -rn
线上运维经验表明,API接口缓存命中率维持在70%以上时,后端服务器负载能降低到原来的1/4。对于SSR渲染页面,缓存命中率还能更高,配合proxy_cache_valid设置合理的TTL,服务器安全加固效果显著。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/nginx-fan-xiang-dai-li-huan-cun-pei-zhi-xiang-jie/