CentOS Apache2 缓存设置技巧
一 核心模块与启用方式
二 磁盘缓存与关键参数
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /static
CacheRoot "/var/cache/httpd/mod_cache_disk"
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheLockPath /var/lock/httpd/mod_cache_lock
CacheLockMaxAge 5
# 可选:忽略查询串,避免 ?v=1 导致重复缓存
CacheIgnoreQueryString On
</IfModule>
</IfModule>
三 浏览器缓存与压缩协同
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/html "access plus 1 hour"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(jpg|jpeg|png|gif|css|js)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
四 连接与监控优化
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
LoadModule status_module modules/mod_status.so
<Location "/server-status">
SetHandler server-status
Require ip 127.0.0.1
</Location>
ExtendedStatus On
访问 http://服务器IP/server-status,关注 CacheHitRatio 等指标,按命中率与业务新鲜度权衡 CacheDefaultExpire/CacheMaxExpire。五 验证与常见问题