您的位置:首页 > 运维架构 > Apache

apache配置静态缓存

2016-03-23 20:08 766 查看
每次访问站点时候,页面有许多图片或者文件占着许多元素,为了节省带宽,可以把静态文件做一个缓存,规定图片等缓存多少天或者多少小时。可以节省一大部分的带宽。

具体操作:
在虚拟主机配置文件中去配置。
[root@No ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "access plus 2 hours"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
:wq
//设置静态文件的缓存。什么文件,缓存多长时间。
然后检测配置文件,重新加载配置文件。
[root@No ~]# apachectl -t
Syntax OK
[root@No ~]# apachectl graceful

现在我们去做一个测试,
在网页上打开discuz论坛,复制一个图片的地址,然后用curl去测试。




# curl -x127.0.0.1:80 'http://www.test.com/static/image/common/logo.png' -I
//查看文件缓存

HTTP/1.1 200 OK
Date: Tue, 17 Nov 2015 12:49:08 GMT
Server: Apache/2.2.24 (Unix) PHP/5.3.27
Last-Modified: Tue, 09 Jun 2015 02:21:10 GMT
ETag: "20c9e-1149-5180c695e1180"
Accept-Ranges: bytes
Content-Length: 4425
Cache-Control: max-age=86400
Expires: Wed, 18 Nov 2015 12:49:08 GMT
Content-Type: image/png

Cache-Control: max-age=86400就是我们设置的缓存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  apache 静态缓存