首页主机资讯Debian Apache缓存如何设置

Debian Apache缓存如何设置

时间2025-11-24 09:56:06发布访客分类主机资讯浏览701
导读:Debian 上 Apache 缓存设置指南 一 核心思路与模块 使用 mod_cache + mod_cache_disk 实现服务器端页面/资源缓存(反向代理或本地内容均可)。 使用 mod_expires + mod_headers...

Debian 上 Apache 缓存设置指南

一 核心思路与模块

  • 使用 mod_cache + mod_cache_disk 实现服务器端页面/资源缓存(反向代理或本地内容均可)。
  • 使用 mod_expires + mod_headers 控制浏览器缓存策略(Cache-Control、Expires)。
  • 可选:使用 mod_file_cache 做文件句柄/元数据层面的内存缓存(适合极少变动的静态文件)。

二 安装与启用模块

  • 安装模块(Debian 包名):
    • sudo apt update
    • sudo apt install libapache2-mod-cache libapache2-mod-cache-disk
    • sudo a2enmod cache cache_disk
    • 如需浏览器缓存:sudo a2enmod expires headers
    • 如需文件句柄缓存:sudo a2enmod file_cache
  • 重启生效:sudo systemctl restart apache2

三 配置服务器端磁盘缓存

  • 准备缓存目录并设置权限(示例路径:/var/cache/apache2/mod_cache_disk):
    • sudo mkdir -p /var/cache/apache2/mod_cache_disk
    • sudo chown -R www-data:www-data /var/cache/apache2/mod_cache_disk
    • sudo chmod -R 755 /var/cache/apache2/mod_cache_disk
  • 全局或虚拟主机配置示例(放在 内或 /etc/apache2/conf-available/cache.conf 后 a2enconf):
    • # 对全站启用磁盘缓存(也可改为 限定路径) CacheEnable disk / CacheRoot "/var/cache/apache2/mod_cache_disk" CacheDirLevels 2 CacheDirLength 1 CacheDefaultExpire 3600 CacheMaxExpire 86400 CacheIgnoreHeaders Set-Cookie CacheIgnoreNoLastMod On CacheIgnoreQueryString On CacheLockOnPost On CacheLockPath /tmp/mod_cache_lock CacheLockMaxAge 5 CacheStorePrivateFiles On # 仅缓存指定 MIME 类型(可按需增减) CacheableMimeType text/html text/css application/javascript image/png image/jpeg image/svg+xml
  • 说明:
    • CacheIgnoreQueryString On 会忽略查询串差异,提升命中率(适合带无意义参数的静态资源)。
    • 对含用户身份信息的页面请勿开启服务器端缓存,或改为更严格的匹配策略。

四 配置浏览器缓存与文件句柄缓存

  • 浏览器缓存(mod_expires + mod_headers,建议对静态资源设置较长 max-age):
    • ExpiresActive On ExpiresDefault "access plus 1 week" ExpiresByType text/html "access plus 1 week" 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"
    • 如需统一覆盖:Header set Cache-Control “public, max-age=31536000”(1 年)
  • 文件句柄/元数据缓存(mod_file_cache,适合极稳定文件):
    • CacheFile /var/www/html/index.html /var/www/html/logo.png
  • 提示:对带指纹/版本号的静态资源(如 app.a1b2c3.js)可设置更长缓存;对 HTML 通常设置较短时间或配合协商缓存。

五 验证与运维

  • 检查模块与配置语法:
    • apache2ctl -M | egrep ‘cache|expires|headers|file_cache’
    • sudo apache2ctl configtest
  • 验证命中与响应头:
    • curl -I https://your-domain/static/app.js
    • 关注:Cache-Control、Expires、Age、X-Cache(命中时常见为 HIT)
  • 日志与目录维护:
    • 观察错误日志:tail -f /var/log/apache2/error.log
    • 定期清理缓存目录或在低峰期重建,避免陈旧内容长期占用。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Debian Apache缓存如何设置
本文地址: https://pptw.com/jishu/754187.html
Debian Apache安全策略有哪些 Debian Apache反向代理怎么配置

游客 回复需填写必要信息