FastCGI 缓存(fastcgi_cache)要点
- 定义缓存区:使用 fastcgi_cache_path(keys_zone、levels、max_size 等)。
- 启用与键:在 location ~ .php$ 中用 fastcgi_cache 指定缓存区;用 fastcgi_cache_key 统一键规则。
- 有效期与状态:用 fastcgi_cache_valid 设置缓存时间;添加 X-Cache-Status 观察命中。
- 示例:
- fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=php_cache:10m;
- location ~ .php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_cache php_cache; fastcgi_cache_valid 200 30m; fastcgi_cache_key $scheme$host$request_uri; add_header X-Cache-Status $upstream_cache_status; }