CentOS LAMP缓存策略优化
一 分层缓存总体策略
二 Apache HTTP Server 缓存配置
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/httpd/mod_cache_disk"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
三 PHP 层缓存配置
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.enable_cli=0
说明:开发环境可开启 validate_timestamps 便于热更新;生产环境可按需调整为定时校验以降低开销。[apcu]
extension=apcu.so
apcu.enable_cli=0
apcu.shm_size=32M
apcu.ttl=7200
apcu.user_ttl=7200
apcu.gc_ttl=3600
; 安装:yum install php-pecl-memcached
[memcached]
extension=memcached.so
memcached.sess_consistency=0
memcached.sess_lock_wait=1500
memcached.sess_lock_peers=1
memcached.sess_lock_peers_timeout=5000
memcached.sess_prefix=phpsess_
如需更高可用与丰富数据结构,可改用 Redis(php-pecl-redis)。四 MariaDB/MySQL 层缓存配置
[mysqld]
innodb_buffer_pool_size=1G
innodb_buffer_pool_instances=8
innodb_log_file_size=256M
innodb_log_buffer_size=16M
建议将 innodb_buffer_pool_size 设为可用内存的较大比例(如 50%–70%,视业务与内存总量而定),以缓存热点数据与索引。五 Varnish 与系统层优化要点
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.method == "PURGE") {
return(synth(200, "Purged"));
}
if (req.http.X-Forwarded-Proto == "https") {
set req.http.X-Forwarded-Proto = "https";
}
return(hash);
}
sub vcl_backend_response {
if (beresp.http.Content-Type ~ "^(text|application)/") {
unset beresp.http.Set-Cookie;
set beresp.ttl = 1h;
}
return(deliver);
}
说明:对可缓存内容设置 beresp.ttl;对带 Set-Cookie 或用户私有内容跳过缓存;按需实现 PURGE 接口与 HTTPS 透传。net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_keepalive_time=1200
net.ipv4.ip_local_port_range="1024 65535"
net.core.somaxconn=1024
net.core.netdev_max_backlog=2000
执行 sysctl -p 使配置生效。