Nginx作为反向代理服务器时,可以通过proxy_cache模块缓存静态资源,显著提高网站性能。当配置了缓存后,Nginx会将后端服务器的响应内容存储在本地,后续相同请求可以直接从缓存中返回,减少后端服务器压力。
Nginx缓存是基于文件系统的,可以直接删除缓存目录中的文件:
# 查找缓存目录位置(通常在nginx.conf中配置)
grep -r "proxy_cache_path" /etc/nginx/
# 清除缓存(假设缓存目录为/var/cache/nginx)
rm -rf /var/cache/nginx/*
Nginx商业版或开源版(需编译)提供了ngx_cache_purge
模块:
location ~ /purge(/.*) {
# 限制访问权限
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge my_cache $1$is_args$args;
}
使用方式:访问 http://example.com/purge/static/js/app.js
清除该文件的缓存
nginx-cache-purge
: Python脚本工具ngx_cache_purge
: 官方模块的第三方实现最佳实践是为静态资源添加版本号或哈希值:
styles.a1b2c3d4.css
app.9e8f7g6h.js
Nginx配置示例:
location ~* \.(css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
# 当后端返回特定头时绕过缓存
proxy_cache_bypass $http_cache_control;
# 添加缓存状态头便于调试
add_header X-Cache-Status $upstream_cache_status;
}
}
map $request_uri $cache_zone {
~\.(js|css|png|jpg|jpeg|gif|ico|woff|woff2|ttf|svg)$ "static_assets";
default "dynamic_content";
}
server {
location / {
proxy_cache $cache_zone;
# 静态资源长期缓存
proxy_cache_valid 200 302 30d;
# 动态内容短期缓存
proxy_cache_valid 200 302 10m;
}
}
nginx
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
nginx
proxy_cache_revalidate on;
添加调试头信息:
add_header X-Cache-Status $upstream_cache_status;
可能的值:MISS, HIT, EXPIRED, UPDATING 等
日志记录缓存状态:
log_format cache_log '$remote_addr - $upstream_cache_status [$time_local] '
'"$request" $status $body_bytes_sent';
使用工具监控缓存命中率:
tail -f /var/log/nginx/access.log | grep -o "X-Cache-Status: .*" | sort | uniq -c
# 定义缓存路径和区域
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=static_assets:10m inactive=365d use_temp_path=off;
proxy_cache_path /var/cache/nginx/dynamic levels=1:2 keys_zone=dynamic_content:10m inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
# 静态资源长期缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|woff|woff2|ttf|svg)$ {
proxy_cache static_assets;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache_valid 200 302 365d;
proxy_cache_valid 404 1m;
expires 1y;
add_header Cache-Control "public, immutable";
add_header X-Cache-Status $upstream_cache_status;
# 源服务器设置
proxy_pass http://backend;
}
# 动态内容短期缓存
location / {
proxy_cache dynamic_content;
proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_bypass $http_cache_control;
proxy_cache_revalidate on;
proxy_cache_lock on;
expires 10m;
add_header X-Cache-Status $upstream_cache_status;
# 源服务器设置
proxy_pass http://backend;
}
# 缓存清理接口
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge static_assets $1;
proxy_cache_purge dynamic_content $1$is_args$args;
}
}
通过以上配置和策略,您可以实现高效的Nginx缓存管理,确保静态资源更新及时生效,同时保持优秀的网站性能。