Apache静态资源缓存需启用mod_expires和mod_headers模块,用ExpiresByType按Content-Type设过期时间,并用Header set补充Cache-Control指令;验证需检查响应头同时含Expires和Cache-Control。
在 apache 主配置文件(如 httpd.conf 或 apache2.conf)中配置静态资源缓存,核心是启用 mod_expires 和 mod_headers 模块,并用 expiresbytype 按 mime 类型设定过期时间,再用 header set 补充现代缓存指令。不推荐只靠文件后缀匹配,更可靠的是基于实际响应的 content-type。
确认并启用必要模块
先确保以下两行在主配置中已取消注释(通常位于 LoadModule 区域):
LoadModule expires_module modules/mod_expires.soLoadModule headers_module modules/mod_headers.so
运行 apachectl -M | grep -E "(expires|headers)" 验证是否加载成功。若未启用,需重启 Apache 生效。
在 VirtualHost 或全局作用域中开启并配置 Expires
在 <VirtualHost> 块内或主配置合适位置添加:
使用ydata-profiling(前身为pandas-profiling)生成全面的数据质量报告,包含相关性分析、缺失值模式和基数检测。导出交互式HTML仪表板和JSON摘要。
<IfModule mod_expires.c>
ExpiresActive On
# 图片类:1年
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
# 样式与脚本:1个月(若带哈希可升为1年)
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
# 字体与 WASM
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType application/wasm "access plus 1 week"
# 禁用动态内容缓存
ExpiresByType text/html "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
ExpiresDefault "access plus 0 seconds"
</IfModule>用 Header set 补充 Cache-Control 提升兼容性
仅靠 Expires 头不足以覆盖所有浏览器行为,必须配合 Cache-Control。继续在同一作用域下添加:
<IfModule mod_headers.c>
# 长效静态资源(图片、字体等)
<FilesMatch "\.(jpe?g|png|gif|webp|ico|svg|woff2|eot|ttf)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
# CSS/JS(若文件名含哈希,建议加 immutable;否则用 max-age=2592000)
<FilesMatch "\.(css|js)$">
Header set Cache-Control "public, max-age=2592000"
</FilesMatch>
# 显式禁止 HTML 缓存
<FilesMatch "\.html?$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>
</IfModule>验证配置是否生效
重启 Apache:sudo systemctl restart apache2(Debian/Ubuntu)或 sudo apachectl restart(RHEL/CentOS)。打开浏览器开发者工具 → Network 标签页,刷新页面后点开任意 JS 或 PNG 文件,检查响应头是否同时包含:
-
Expires: [未来 GMT 时间](如Tue, 22 Jul 2027 00:00:00 GMT) Cache-Control: public, max-age=..., immutable
两项都存在,说明配置已正确生效。

















