Skip to content

缓存清理策略

合理的缓存清理策略可以确保缓存的有效性和及时更新。

手动清理

删除所有缓存

bash
rm -rf /var/cache/nginx/*

删除特定缓存

bash
# Proxy Cache
find /var/cache/nginx -name "*.cache" -delete

# FastCGI Cache
find /var/cache/nginx/fastcgi -name "*.cache" -delete

删除过期缓存

bash
# 删除超过7天的缓存
find /var/cache/nginx -type f -mtime +7 -delete

自动清理

Cron定时清理

bash
sudo crontab -e

添加以下内容:

0 0 * * * find /var/cache/nginx -type f -mtime +7 -delete

Shell脚本清理

bash
#!/bin/bash
# 清理过期缓存
find /var/cache/nginx -type f -mtime +7 -delete

# 清理空目录
find /var/cache/nginx -type d -empty -delete

设置执行权限:

bash
chmod +x /var/www/clean_cache.sh

添加到Cron:

bash
0 0 * * * /var/www/clean_cache.sh

缓存清除模块

安装ngx_cache_purge模块

需要重新编译Nginx,添加ngx_cache_purge模块。

Proxy Cache清除

nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=proxy_cache:10m max_size=1g inactive=60m;

server {
    listen 80;
    server_name proxy.example.com;

    location / {
        proxy_cache proxy_cache;
        proxy_pass http://backend;
    }

    location /cache/ {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge proxy_cache $scheme$request_method$host$request_uri;
    }
}

FastCGI Cache清除

nginx
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fastcgi_cache:10m max_size=1g inactive=60m;

server {
    listen 80;
    server_name www.example.com;

    location ~ \.php$ {
        fastcgi_cache fastcgi_cache;
        fastcgi_pass 127.0.0.1:9000;
    }

    location /cache/ {
        allow 127.0.0.1;
        deny all;
        fastcgi_cache_purge fastcgi_cache $scheme$request_method$host$request_uri;
    }
}

缓存更新策略

短期缓存

nginx
proxy_cache_valid 200 1m;

中期缓存

nginx
proxy_cache_valid 200 60m;

长期缓存

nginx
proxy_cache_valid 200 7d;

条件缓存

基于URL的缓存

nginx
location ~* \.(jpg|jpeg|png|gif|css|js)$ {
    proxy_cache proxy_cache;
    proxy_cache_valid 200 7d;
    proxy_pass http://backend;
}

基于Cookie的缓存

nginx
set $skip_cache 0;
if ($http_cookie ~* "wordpress_logged_in") {
    set $skip_cache 1;
}

location / {
    proxy_cache_bypass $skip_cache;
    proxy_no_cache $skip_cache;
    proxy_pass http://backend;
}

基于请求方法的缓存

nginx
location / {
    proxy_cache proxy_cache;
    proxy_cache_methods GET HEAD;
    proxy_pass http://backend;
}

缓存预热

手动预热

bash
# 预热首页
curl http://example.com/

# 预热特定页面
curl http://example.com/page1
curl http://example.com/page2

脚本预热

bash
#!/bin/bash
# 预热网站
urls=(
    "http://example.com/"
    "http://example.com/page1"
    "http://example.com/page2"
)

for url in "${urls[@]}"; do
    echo "Warming: $url"
    curl -s -o /dev/null $url
done

缓存监控

查看缓存大小

bash
du -sh /var/cache/nginx

查看缓存文件数

bash
find /var/cache/nginx -type f | wc -l

查看缓存命中率

nginx
log_format cache '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$upstream_cache_status"';

access_log /var/log/nginx/cache.log cache;

完整示例

生产环境配置

nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=proxy_cache:10m max_size=1g inactive=60m;

server {
    listen 80;
    server_name proxy.example.com;

    # 静态资源缓存
    location ~* \.(jpg|jpeg|png|gif|css|js)$ {
        proxy_cache proxy_cache;
        proxy_cache_valid 200 7d;
        proxy_cache_key "$scheme$request_method$host$request_uri";
        proxy_pass http://backend;

        add_header X-Cache-Status $upstream_cache_status;
    }

    # API缓存
    location /api/ {
        proxy_cache proxy_cache;
        proxy_cache_valid 200 60m;
        proxy_cache_valid 404 1m;
        proxy_cache_key "$scheme$request_method$host$request_uri";
        proxy_pass http://backend;

        add_header X-Cache-Status $upstream_cache_status;
    }

    # 缓存清除
    location /cache/ {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge proxy_cache $scheme$request_method$host$request_uri;
    }
}

常见问题

缓存未清理

原因: 清理脚本未正确执行

解决: 检查Cron配置

bash
sudo crontab -l

缓存清理失败

原因: 权限不足

解决: 设置正确的权限

bash
sudo chown -R nginx:nginx /var/cache/nginx

缓存命中率低

原因: 缓存时间过短

解决: 延长缓存时间

nginx
proxy_cache_valid 200 60m;

总结

缓存清理策略的关键点:

  • 手动清理:定期删除过期缓存
  • 自动清理:使用Cron定时清理
  • 缓存清除模块:精确清除特定缓存
  • 缓存更新策略:设置合理的缓存时间
  • 缓存预热:提前加载热门内容
  • 缓存监控:监控缓存使用情况

合理配置缓存清理策略,确保缓存的有效性和及时更新。