apache 2: 让浏览器缓存图片等 ( apache make browser cache assets )

访问量: 1025

refer to:  http://metaskills.net/2006/02/19/how-to-control-browser-caching-with-apache-2/

apache 有点旧,但是我对它的印象还不错,就是操作上比nginx多做了一丢丢。

有个应用,

1. 前提: 先看看 apache 的设置,确保 enable 了 headers, expires

$ a2enmod headers
$ a2enmod expires
$ service apache2 restart 

2. 在对应的配置文件中,加上内容:

<VirtualHost *:80>

    ....
     <Directory "/opt/your_app">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        # add this line: 
        ExpiresDefault "access plus 30 days"
     </Directory>
</VirtualHost>

或者,在对应的 virtualhost 目录下的 .htaccess中:(完整代码见:http://stackoverflow.com/questions/12497601/force-browser-cache-css-and-js-files-gzipped-with-apache-php

<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifModule>

以及下面的部分:

<ifModule mod_headers.c>

<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
  Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>


</ifModule>

订阅/RSS Feed

Subscribe

分类/category