开启浏览器缓存是指访客第一次访问时下载的 css 和 js 文件存放在访客本地电脑,当访客第二次访问网站时,浏览器无需下载 css 和 js 文件,直接调用本地 css 和 js 文件,加快了网页的加载。
Apache 服务器开启浏览器缓存的办法
LAMP 服务器上,编辑网站根目录下的.htaccess 文件,加上如下内容:
## EXPIRES CACHING ##ExpiresActiveOnExpiresByType image/jpg "access 1 year"ExpiresByType image/jpeg "access 1 year"ExpiresByType image/gif "access 1 year"ExpiresByType image/png "access 1 year"ExpiresByType text/css "access 1 month"ExpiresByType application/pdf "access 1 month"ExpiresByType text/x-javascript "access 1 month"ExpiresByType application/x-shockwave-flash "access 1 month"ExpiresByType image/x-icon "access 1 year"ExpiresDefault"access 1 days"## EXPIRES CACHING ##
这就基本上缓存了大部分较大的文件。
Nginx 服务器开启浏览器缓存的办法
在网站的配置文件 bo.moioi.com.conf 里的 location 段编辑成如下的内容:
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
然后重启 nginx 服务 service nginx restart,大功告成。
expires 365d;
}location ~* .(pdf)$ {
expires 30d;
}
WordPress 网站启用浏览器缓存可以提高 google pagespeed test 的分数。