LNMP缓存技术全景与落地方案
一、分层缓存架构与适用场景
二、关键配置示例
http {
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=my_fcgi_cache:10m
max_size=1g inactive=60m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_cache my_fcgi_cache;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404 1m;
add_header X-FastCGI-Cache $upstream_cache_status;
}
}
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
[apcu]
apcu.enable=1
apcu.shm_size=64M
apcu.ttl=7200
# Redis
sudo apt install redis-server php-redis
# Memcached
sudo apt install memcached php-memcached
以上示例涵盖proxy_cache/fastcgi_cache路径与头部、静态资源过期、OPcache/APCu关键参数及Redis/Memcached安装扩展步骤。三、缓存策略与一致性设计
四、监控、验证与运维要点
五、选型建议与注意事项