【发布时间】:2011-05-07 16:46:27
【问题描述】:
我最近在 firebug 上使用 pagespeed 插件分析了我的网站。它建议我对 CSS、JS 和图像文件设置过期时间。
我想知道,我该怎么做?
【问题讨论】:
-
这真的取决于您使用的平台:IIS6/7、Apache 等。
-
我使用 Apache 作为我的网络服务器。我通过我的 php 文件或在 php 文件中显示这些文件
我最近在 firebug 上使用 pagespeed 插件分析了我的网站。它建议我对 CSS、JS 和图像文件设置过期时间。
我想知道,我该怎么做?
【问题讨论】:
Add web.config file in your solution to remove "expiration not specified"
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
【讨论】:
我想为那些搜索它的人添加这个解决方案....
它也很好用...使用.htaccess
https://webmasters.stackexchange.com/a/5275/37765
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault A2592000
</FilesMatch>
【讨论】:
ln -s /etc/apache2/mods-available/expires.load /etc/apache2/mods-enabled/(找到 here)虽然模块已经启用 a2enmod headers 我有 500
更好的一个(在http://www.paulund.co.uk/set-expire-headers-in-htaccess找到,但由于0 seconds不起作用,我已将其更改为1 seconds)
# These are pretty far-future expires headers
# They assume you control versioning with cachebusting query params like: <script src="application.js?20100608">
# Additionally, consider that outdated proxies may miscache
#
# www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
# If you don`t use filenames to version, lower the css and js to something like "access plus 1 week"
<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 1 seconds"
# Your document html
ExpiresByType text/html "access plus 1 seconds"
# Data
ExpiresByType text/xml "access plus 1 seconds"
ExpiresByType application/xml "access plus 1 seconds"
ExpiresByType application/json "access plus 1 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 1 seconds"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
【讨论】:
我所做的是创建一个文件“expires.conf”并将其包含在 Apache 的站点文件配置中。如果需要,您可以包含在 .htaccess 中。 我的过期时间:
<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 text/html "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 1 month"
</IfModule>
你需要在apache中激活expires模块。
【讨论】:
这是我在运行 PageSpeed 插件时用来修复完全相同的问题的那个:
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
这会进入您的 .htaccess 文件。
阅读此页面以获取有关如何为其他文件类型设置缓存和/或更改缓存长度的更多信息:
http://www.askapache.com/htaccess/apache-speed-cache-control.html
【讨论】: