指定静态文件高速缓存的过期日期

时间:2011-04-03 19:44:08

标签: apache caching cache-expiration

当我测试我的网站SpeedTest时,我发现很多过期未指定错误。您可以在this page看到。

我将此代码添加到我的.htaccess文件

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType 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 2 days"
</IfModule>
## EXPIRES CACHING ##

但没有改变。你能给我一个建议吗,我可以为静态文件指定过期日期吗?

服务器:Linux - Apache

5 个答案:

答案 0 :(得分:37)

我喜欢html5boilerplate方式:

<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"

# your document html 
  ExpiresByType text/html                 "access plus 0 seconds"

# data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"

# rss feed
  ExpiresByType application/rss+xml       "access plus 1 hour"

# favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week" 

# media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"

# htc files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"

# webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# css and javascript
  ExpiresByType text/css                  "access plus 2 months"
  ExpiresByType application/javascript    "access plus 2 months"
  ExpiresByType text/javascript           "access plus 2 months"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

希望这对你有用。 资源: https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

答案 1 :(得分:5)

你可以使用mod_headers来解决这个问题:

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
  Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

或者您可以使用mod_expires:

ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"

答案 2 :(得分:1)

我看到你安装了PHP(参考wordpress),所以我们会尝试诊断你的问题:

  1. 重新启动您的Apache Web服务器。如果您拥有apache服务器(命令行访问权限),则可以执行like such
  2. create a phpinfo page。这将允许您查看已安装的apache模块。检查您想要使用的mod_expires.c模块(应该能够在浏览器中为mod_expires执行CTL-F(查找)请求)。如果你找到它 - 好!如果没有,则需要在apache服务器中安装mod_expires。如果您拥有Apache服务器,则可以搜索如何为特定操作系统安装模块。如果没有,您可以咨询您的托管服务提供商,了解如何安装此模块。安装完成后,继续。
  3. 修改您的http.conf条目,在表达式中包含“plus”
  4. 再次重启Apache
  5. 测试=)

答案 3 :(得分:1)

你只需要添加:

ExpiresByType application/javascript "access plus 1 year"

答案 4 :(得分:0)

也许你没有加载mod_expires,所以整个节是无操作的。或者,AllowOverride为None,并且不处理.htaccess文件。