debian

如何优化Debian Apache2缓存

小樊
33
2025-12-07 23:16:32
栏目: 智能运维

Debian Apache2 缓存优化实操指南

一 核心思路与模块

二 推荐配置示例

# 1) 浏览器缓存:强缓存 + 协商缓存
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 day"
    ExpiresByType text/html       "access plus 1 hour"
    ExpiresByType text/css        "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/jpeg     "access plus 1 year"
    ExpiresByType image/png      "access plus 1 year"
    ExpiresByType image/svg+xml  "access plus 1 year"
    ExpiresByType font/woff2     "access plus 1 year"
</IfModule>

# 2) 反向代理/磁盘缓存(网关缓存)
<IfModule mod_cache.c>
  <IfModule mod_cache_disk.c>
    CacheRoot "/var/cache/apache2/mod_cache_disk"
    CacheEnable disk  /
    CacheDirLevels 2
    CacheDirLength 1
    CacheDefaultExpire 3600
    CacheMaxExpire 86400
    CacheMinExpire 600

    # 可按需调整:对隐私/个性化内容谨慎放开
    CacheIgnoreHeaders Set-Cookie
    CacheIgnoreNoLastMod On
    CacheIgnoreQueryString On
    CacheLockOnPost On
    CacheLockPath /tmp/mod_cache_lock
    CacheLockMaxAge 5
    CacheStorePrivateFiles Off
    CacheableMimeType text/html text/css application/javascript
  </IfModule>
</IfModule>

# 3) 压缩传输(减少带宽与时延)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>

# 4) 静态文件句柄缓存(可选,适合极少变更的“热点”文件)
<IfModule mod_file_cache.c>
    CacheFile /var/www/html/index.html
    CacheFile /var/www/html/static/logo.png
</IfModule>

三 按资源类型设置缓存时长

四 验证与监控

五 常见问题与排查

0
看了该问题的人还看了