静态资源是可以通过 缓存设置来加速读取的. 设置方法很简单 但首先你得在 apache2 服务器里把 相关的 模块 开启了 i.e. header.
1 | a2enmod headers |
a2enmod headers
这个模块是用于设置静态资源缓存的 如果没开启 你将会获得 500 服务器内部错误.
Module headers already enabled
然后 在每个网站的 根目录 .htaccess 文件里 加入 以下内容 并保证 apache2 对于 mod_rewrite 的设置是 Override All 这样子目录的 设置会从根目录下继承.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 1 Month for all your static assets 一个月 <FilesMatch ".(ico|pdf|bmp|vbs|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> # 1 DAYS for rss feeds and robots 一天 <FilesMatch ".(xml|txt)$"> Header set Cache-Control "max-age=86400, public, must-revalidate> </FilesMatch> # 8 HOURS for your real articles files 8小时 <FilesMatch ".(html|htm)$"> Header set Cache-Control "max-age=28800, must-revalidate> </FilesMatch> |
# 1 Month for all your static assets 一个月 <FilesMatch ".(ico|pdf|bmp|vbs|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> # 1 DAYS for rss feeds and robots 一天 <FilesMatch ".(xml|txt)$"> Header set Cache-Control "max-age=86400, public, must-revalidate> </FilesMatch> # 8 HOURS for your real articles files 8小时 <FilesMatch ".(html|htm)$"> Header set Cache-Control "max-age=28800, must-revalidate> </FilesMatch>
过期时间 是用秒来记录的. 这样 APACHE 服务器就会告诉你的浏览器 资源还没过期不需要重新下载 而是尽可能的去本地找之前已经缓存好的, 这样一是减少服务器负担 另一是加快页面读取时间.
最后 保存之后 记得要重启服务器.
1 | sudo /etc/init.d/apache2 restart |
sudo /etc/init.d/apache2 restart
或者:
1 | sudo service apache2 restart |
sudo service apache2 restart
效果真的非常好!
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK