52
Add Expires headers
There are 21 static components without a far-future expiration date.

    http://static.doers.lk/examples-offline.css
    http://static.doers.lk/kendo.common.min.css
    http://static.doers.lk/kendo.default.min.css
    http://static.doers.lk/style.css
    http://static.doers.lk/jquery.min.js
    http://static.doers.lk/kendo.web.min.js
    http://static.doers.lk/console.js
    http://static.doers.lk/sprite.png
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    http://static.doers.lk/favicon.PNG

使用 yahoo YSLOW 测试站点时,它会显示上述消息。所以我不知道如何添加过期标头。任何帮助,将不胜感激?

4

5 回答 5

73

添加这些标头的最简单方法是.htaccess向您的服务器添加一些配置的文件。如果资产托管在您无法控制的服务器上,则您无能为力。

请注意,某些托管服务提供商不允许您使用.htaccess文件,因此如果它似乎不起作用,请检查他们的条款。

HTML5Boilerplate 项目有一个很好的.htaccess文件,涵盖了必要的设置。在他们的Github 存储库中查看文件的相关部分

这些是重要的位

# ----------------------------------------------------------------------
# Expires headers (for better cache control)
# ----------------------------------------------------------------------

# These are pretty far-future expires headers.
# They assume you control versioning with filename-based cache busting
# 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

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

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

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
</IfModule>

他们已经记录了该文件的作用,最重要的一点是您需要在 CSS 和 Javascript 文件更改时重命名它们,因为一旦它们被缓存,您的访问者的浏览器将不会在一年内再次检查它们。

于 2012-12-20T12:54:04.727 回答
12

试试这个解决方案,它对我来说很好用

## 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 text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/x-js text/js 
</IfModule>

## EXPIRES CACHING ##
于 2014-07-31T05:26:25.983 回答
3

您可以将它们添加到您的 htaccess 文件或 vhost 配置中。

见这里:http ://httpd.apache.org/docs/2.2/mod/mod_expires.html

但除非您拥有这些域名.. 它们是我们的您的控制。

于 2012-12-20T12:53:40.200 回答
0
<IfModule mod_expires.c>
    # Enable expirations
    ExpiresActive On 

    # Default directive
    ExpiresDefault "access plus 1 month"

    # My favicon
    ExpiresByType image/x-icon "access plus 1 year"

    # Images
    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"

    # CSS
    ExpiresByType text/css "access plus 1 month"

    # Javascript
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>
于 2017-05-18T13:04:13.787 回答
0

在 ASP.NET 中也有类似的对象,可以使用 WebFormsUserControls 中的 Caching Portions 来缓存页面对象一段时间,节省服务器资源。这也称为片段缓存。
如果将此代码包含在用户控件的顶部,则该控件的版本将在输出缓存中存储 150 秒。您可以创建自己的控件,该控件将包含所需特定资源的过期标头。

<%@ OutputCache Duration="150" VaryByParam="None" %>

这篇文章完全解释了它: ASP.NET 页面的缓存部分

于 2019-02-27T16:28:44.373 回答