ukula 发表于 2018-11-19 07:40:51

Apache配置——静态缓存

  访问一个网站的时候,里面有各种各样的小图片,这些在每个页面里有很多的元素,它们所占用的空间也很大,那我们为了把网页传输的大小变小,可以把静态文件在浏览器这一侧做个缓存,在apache服务端可以配置的,规定它缓存多少天,多少个小时,等过了那些小时,它再重新请求、刷新,也就意味着我们的网站会有很多次请求,每次请求它就不会在服务端调用它,这样就会节省很大的带宽资源。
  访问很慢:北京-->U2B
  解决方案:北京-->代理服务器(缓存U2B上的静态文件)-->U2B
  ## 编辑虚拟主机配置文件
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
     
        ExpiresActive on
        ExpiresByType image/gif "access plus 1 days"
        ExpiresByType image/jpeg "access plus 24 hours"
        ExpiresByType image/png " now plus 24 hours"
        ExpiresByType text/css "now plus 2 hours"
        ExpiresByType application/x-javascript "now plus 2 hours"
        ExpiresByType application/javascript "now plus 2 hours"
        ExpiresByType application/x-shockwave-flash "now plus 2 hours"
        ExpiresDefault "now plus 0 min"
     
  解释说明:
  在匹配.js文件时候需要写两行javascript与x-javascript
http://note.youdao.com/yws/res/4879/WEBRESOURCEfe3a56e4f3e99c05ab40406c65850edf
  ## 检查并重新加载
  # /usr/local/apache2/bin/apachectl -t
  Syntax OK
  # /usr/local/apache2/bin/apachectl graceful
  ## 测试gif(设置为1天的时候)
  # curl -xlocalhost:80 http://www.test.com/static/image/common/online_supermod.gif -I
  HTTP/1.1 200 OK
  Date: Sat, 05 Nov 2016 16:00:11 GMT
  Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
  Last-Modified: Thu, 04 Sep 2014 03:22:34 GMT
  ETag: "20a62-17c-50234dd113280"
  Accept-Ranges: bytes
  Content-Length: 380
  Cache-Control: max-age=86400
  Expires: Sun, 06 Nov 2016 16:00:11 GMT
  Content-Type: image/gif
  ## 测试gif(设置为2天的时候)
  # curl -xlocalhost:80 http://www.test.com/static/image/common/online_supermod.gif -I
  HTTP/1.1 200 OK
  Date: Sat, 05 Nov 2016 16:03:23 GMT
  Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
  Last-Modified: Thu, 04 Sep 2014 03:22:34 GMT
  ETag: "20a62-17c-50234dd113280"
  Accept-Ranges: bytes
  Content-Length: 380
  Cache-Control: max-age=172800
  Expires: Mon, 07 Nov 2016 16:03:23 GMT
  Content-Type: image/gif
  或者使用mod_headers模块方式配置缓存静态文件
     
        
              header set cache-control "max-age=3600"
        
        
              header set cache-control "max-age=604800"
        
        
              header set cache-control "max-age=31536000"
        
     
  解释说明:
  max-age=3600 表示一个小时
  "\.(css|js|swf).*" 因为有.css?的形式
  




页: [1]
查看完整版本: Apache配置——静态缓存