静态文件缓存
静态缓存在客户端下进行缓存,可以设置缓存文件类型与缓存时间,提升客户端访问站点速度。
语法格式
ExpiresByType type/encoding “<base> [plus] <num><type>”
配置静态缓存
1、模块解注释
vim /apache2/conf/httpd.confLoadModule expires_module modules/mod_expires.so
2、主配置文件内编辑,它是一个全局配置。
vim /apache2/conf/httpd.conf<IfModule mod_expires.c> ??# 开启使用expires ??ExpiresActive on ??# 指定gif 文件保存1天 image触发源/类型 ??ExpiresByType image/gif "access plus 1 days" ??# 指定jpeg 文件保存24小时 ??ExpiresByType image/jpeg "access plus 24 hours" ??# 指定png 文件保存24小时 ??ExpiresByType image/png "access plus 24 hours" ??# 指定css 文件保存2小时 ??ExpiresByType test/css "now plus 2 hour" ??# 指定javascript 文件保存2小时 ??ExpiresByType application/x-javascript "now plus 2 hours" ??# 指定flash 文件保存2小时 ??ExpiresByType application/x-shockwave-flash "now plus 2 hours" ??# 处理上述文件 其他都保存0秒(不保存) ??ExpiresDefault "now plus 0 min"</IfModule>
3、加载配置文件
/usr/local/apache2/bin/apachectl graceful
测试静态缓存
1、火狐浏览器测试
火狐浏览器-->F12-->网络-->304文件-->消息头-->响应头-->Cache-Control:max-age=86400(缓存时间)
2、Linux系统下通过curl 测试 加载的是图片 需要加 -I
curl -x192.168.1.107:80 ‘http://192.168.1.107/static/image/common/logo.png‘ -I
HTTP/1.1 200 OKDate: Tue, 23 Jan 2018 14:44:10 GMTServer: Apache/2.4.27 (Unix) PHP/5.3.22Last-Modified: Tue, 31 May 2016 03:08:36 GMTETag: "1149-5341ab0597500"Accept-Ranges: bytesContent-Length: 4425Cache-Control: max-age=86400Expires: Wed, 24 Jan 2018 14:44:10 GMTContent-Type: image/png注:304 调用了本地的缓存文件注:curl 200 不会显示 304注:max-age=86400 缓存时间注:Expires: Wed, 24 Jan 2018 14:44:10 GMT 过期时间
Apache 静态缓存配置
原文地址:https://www.cnblogs.com/xiangsikai/p/8371866.html