Skip to content

FastCGI Cache配置

FastCGI Cache可以缓存PHP等FastCGI应用的响应,提高动态网站性能。

基本配置

定义缓存路径

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

启用缓存

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

完整配置

生产环境配置

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;

    root /var/www/html;
    index index.php index.html;

    location ~ \.php$ {
        fastcgi_cache fastcgi_cache;
        fastcgi_cache_valid 200 60m;
        fastcgi_cache_valid 404 1m;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        add_header X-Cache-Status $upstream_cache_status;
    }
}

缓存参数

fastcgi_cache_path参数

levels

  • 缓存目录层级
  • 格式:1:2表示两级目录

keys_zone

  • 缓存键共享内存区域
  • 格式:名称:大小

max_size

  • 最大缓存大小
  • 超过后自动清理旧缓存

inactive

  • 非活动时间
  • 超时后删除缓存

fastcgi_cache_valid参数

nginx
fastcgi_cache_valid 200 60m;
fastcgi_cache_valid 404 1m;

说明:

  • 200响应缓存60分钟
  • 404响应缓存1分钟

缓存键

默认缓存键

nginx
fastcgi_cache_key "$scheme$proxy_host$request_uri";

自定义缓存键

nginx
fastcgi_cache_key "$scheme$request_method$host$request_uri";

基于Cookie的缓存键

nginx
fastcgi_cache_key "$scheme$request_method$host$request_uri$is_args$args";

缓存控制

禁用缓存

nginx
location ~ \.php$ {
    fastcgi_no_cache 1;
    fastcgi_pass 127.0.0.1:9000;
}

条件缓存

nginx
location ~ \.php$ {
    fastcgi_cache_bypass $http_cache_control;
    fastcgi_pass 127.0.0.1:9000;
}

缓存特定状态码

nginx
location ~ \.php$ {
    fastcgi_cache_valid 200 302 60m;
    fastcgi_cache_valid 404 1m;
    fastcgi_pass 127.0.0.1:9000;
}

缓存清理

手动清理

bash
# 删除所有缓存
rm -rf /var/cache/nginx/fastcgi/*

# 删除特定缓存
find /var/cache/nginx/fastcgi -name "*.cache" -delete

缓存清除模块

nginx
location /cache/ {
    fastcgi_cache_purge $purge_method;
}

缓存监控

查看缓存状态

nginx
add_header X-Cache-Status $upstream_cache_status;

缓存状态值

  • MISS:未命中缓存
  • BYPASS:绕过缓存
  • EXPIRED:缓存过期
  • STALE:使用过期缓存
  • UPDATING:正在更新缓存
  • HIT:命中缓存

完整示例

WordPress配置

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;

    root /var/www/wordpress;
    index index.php index.html;

    # 不缓存登录和后台
    set $skip_cache 0;
    if ($request_method = POST) {
        set $skip_cache 1;
    }
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $skip_cache 1;
    }

    location ~ \.php$ {
        fastcgi_cache fastcgi_cache;
        fastcgi_cache_valid 200 60m;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        add_header X-Cache-Status $upstream_cache_status;
    }
}

常见问题

缓存不生效

原因: PHP返回禁止缓存的头

解决: 忽略缓存头

nginx
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

缓存未更新

原因: 缓存时间过长

解决: 缩短缓存时间或手动清理

nginx
fastcgi_cache_valid 200 60m;

登录状态被缓存

原因: 未排除登录页面

解决: 排除登录和后台页面

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

总结

FastCGI Cache配置的关键点:

  • fastcgi_cache_path:定义缓存路径和参数
  • fastcgi_cache:启用缓存
  • fastcgi_cache_valid:设置缓存时间
  • fastcgi_cache_key:设置缓存键
  • 缓存控制:禁用或条件缓存
  • 缓存监控:查看缓存状态

合理配置FastCGI Cache,提高动态网站性能。