【问题标题】:how to set up both httpexpires and cachecontrol headers如何设置 httpexpires 和 cachecontrol 标头
【发布时间】:2014-01-16 12:59:46
【问题描述】:

我想在 web.config 中设置 expires 和 cachecontrolhttpExpires 标头 按照该问题的答案 What's the difference Expires and Cache-control:max-age?

     <system.webServer>
        <staticContent>
            <clientCache cacheControlCustom="public" cacheControlMaxAge="12:00:00" cacheControlMode="UseMaxAge" />
            <clientCache cacheControlCustom="public" httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" cacheControlMode="UseExpires" />
        </staticContent>
    </system.webServer>

但由于某种原因,当我这样做时,图像变得不可用。

每次图像加载时我都有Failed to load resource: the server responded with a status of 500 (Internal Server Error) 错误(我可以在browser dev tools console 中看到错误)。

我猜我配置错了?

如果我只留下一个评论 clientCache 部分的任何部分,它会起作用

如何解决?

更新: 刚刚又问了一个相关问题how to set up both httpexpires and cachecontrol headers web.cofig

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 我在使用UseExpiresUseMaxAge 时遇到了同样的问题,您找到任何解决方案了吗,或者我们可以同时使用吗?

标签: asp.net asp.net-mvc http-headers cache-control


【解决方案1】:

IIS Client Cache page 声明“虽然“Expires”和“max-age”设置有些类似,但“max-age”指令优先于“Expires”。但是,当一个“max-age”clientCache 条目与一个“Expires”clientCache 条目并行使用时,IIS 会生成 HTTP 500 错误。

在 IIS 中调整“Set Common Headers”时,“Expires”和“max-age”相互排斥。您可以使用其中一种,但不能同时使用。

其他缓存指令可以应用于 Web.config 中的子文件夹或特定文件。请参阅此stackoverflow page 在 IIS7 中配置缓存内容。

【讨论】:

  • 你可以使用一个或另一个,但我可以看到堆栈溢出为他们的图像和 css 等设置了“过期”和“最大年龄”,比如:Cache-Control:public, max-age=60Expires:Sun, 13 Mar 2016 08:05:18 GMT 他们是怎么做的那 ?当我在 web.config 中同时使用两者时,我得到 500 错误内部服务器错误。
【解决方案2】:
<staticContent>
        <clientCache cacheControlCustom="public;max-age" cacheControlMode="UseMaxAge" cacheControlMaxAge="10.00:00:00" />
</staticContent>
<urlCompression dynamicCompressionBeforeCache="true" />

这对我来说很好,可以将请求缓存 10 天。

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多