【问题标题】:Expires headers are being ignored by browsers浏览器忽略过期标头
【发布时间】:2016-06-21 15:27:29
【问题描述】:

我已在我的 .htaccess 文件中添加了过期标头,但它们似乎并没有真正起作用。

在 Firefox 和 Chrome 中检查结果时(按 F12,选择网络选项并按 F5 重新加载页面),我注意到我在文件中添加的规则被忽略了。

这些是规则,抄自boilerplate

# ----------------------------------------------------------------------
# | ETags                                                              |
# ----------------------------------------------------------------------

# Remove `ETags` as resources are sent with far-future expires headers.
#
# https://developer.yahoo.com/performance/rules.html#etags
# https://tools.ietf.org/html/rfc7232#section-2.3

# `FileETag None` doesn't work in all cases.
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>

FileETag None

# ----------------------------------------------------------------------
# | Expires headers                                                    |
# ----------------------------------------------------------------------

# Serve resources with far-future expires headers.
#
# (!) If you don't control versioning with filename-based
# cache busting, you should consider lowering the cache times
# to something like one week.
#
# https://httpd.apache.org/docs/current/mod/mod_expires.html

<IfModule mod_expires.c>

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"

  # CSS

    ExpiresByType text/css                              "access plus 1 year"


  # Data interchange

    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rdf+xml                   "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"

    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/ld+json                   "access plus 0 seconds"
    ExpiresByType application/schema+json               "access plus 0 seconds"
    ExpiresByType application/vnd.geo+json              "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"


  # Favicon (cannot be renamed!) and cursor images

    ExpiresByType image/vnd.microsoft.icon              "access plus 1 week"
    ExpiresByType image/x-icon                          "access plus 1 week"

  # HTML

    ExpiresByType text/html                             "access plus 0 seconds"


  # JavaScript

    ExpiresByType application/javascript                "access plus 1 year"
    ExpiresByType application/x-javascript              "access plus 1 year"
    ExpiresByType text/javascript                       "access plus 1 year"


  # Manifest files

    ExpiresByType application/manifest+json             "access plus 1 week"
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"


  # Media files

    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/bmp                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 year"
    ExpiresByType image/jpeg                            "access plus 1 year"
    ExpiresByType image/png                             "access plus 1 year"
    ExpiresByType image/svg+xml                         "access plus 1 month"
    ExpiresByType image/webp                            "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"


  # Web fonts

    # Embedded OpenType (EOT)
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType font/eot                              "access plus 1 month"

    # OpenType
    ExpiresByType font/opentype                         "access plus 1 month"

    # TrueType
    ExpiresByType application/x-font-ttf                "access plus 1 month"

    # Web Open Font Format (WOFF) 1.0
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/x-font-woff               "access plus 1 month"
    ExpiresByType font/woff                             "access plus 1 month"

    # Web Open Font Format (WOFF) 2.0
    ExpiresByType application/font-woff2                "access plus 1 month"


  # Other

    ExpiresByType text/x-cross-domain-policy            "access plus 1 week"

</IfModule>

这些是浏览器中显示的标题。如您所见,脚本和样式表等组件在一个月后过期,而图像在两个月后过期。

样式表标题:

Cache-Control: max-age=2592000, public
Content-Encoding: gzip
Content-Type: text/css
Date: Tue, 21 Jun 2016 16:06:22 GMT
Expires: Thu, 21 Jul 2016 16:06:22 GMT

脚本头:

Cache-Control: max-age=2592000, public
Content-Encoding: gzip
Content-Type: application/javascript
Date: Tue, 21 Jun 2016 16:06:22 GMT
Expires: Thu, 21 Jul 2016 16:06:22 GMT

图片标题:

Cache-Control: max-age=5184000, public
Connection: keep-alive
Date: Tue, 21 Jun 2016 16:10:13 GMT
Expires: Sat, 20 Aug 2016 16:10:13 GMT

对此我有什么办法吗?

【问题讨论】:

  • " 我注意到我在文件中添加的规则被忽略了。"您能否详细说明这部分?你是怎么注意到的?
  • @FlorianLemaitre 你说得对,我已经添加了更多关于此的信息。谢谢。

标签: .htaccess browser-cache expires-header


【解决方案1】:

首先,使用 curl -IL "(URL)" 确认 Web 服务器发送的确切标头。

其次,确保您没有在 Chrome 中选中“忽略缓存”复选框。

第三,当从 curl、Chrome 和 Firefox 进行访问时,访问日志对这些请求有何说明?

编辑:实际的答案是检查是否实际启用了 expires 模块。

【讨论】:

  • 1.该命令返回以下信息:Last-Modified: Mon, 20 Jun 2016 18:08:38 GMT。缓存控制:max-age=0。到期:格林威治标准时间 2016 年 6 月 21 日星期二 18:54:03。 2. 不,我没有检查过.. 3. 我使用 curl -v,它返回了这个:日期:星期二,2016 年 6 月 21 日 19:12:34 GMT。连接:保持活动。最后修改时间:2016 年 6 月 20 日星期一 18:08:38 GMT。缓存控制:max-age=0。到期:格林威治标准时间 2016 年 6 月 21 日星期二 19:12:34。 Firefox 上的附加 HTTP 请求记录器仅显示我访问该站点时下载的文件。你能告诉我是否有一些更有用的方式来查看日志吗?
  • 所以我认为您没有安装或启用 mod_expires.c 模块。确保您已启用模块 mod_expires.c ,如此处所述 absolutelytech.com/2010/08/02/…
  • 我的托管服务提供商应该默认启用它,但他们说他们肯定会检查它。所以我想我将不得不等待回复。同时,非常感谢您。
  • 您可以自己查看。只需创建一个包含&lt;?php phpinfo();phpinfo.php 文件并检查此页面上启用的模块。
  • @FlorianLemaitre 我已经收到了他们的回复,但是下次我需要检查已启用的模块时,我一定会使用您的建议。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多