如何申请 <clientCache/>设置到 IIS 中的特定扩展? [英] How can I apply <clientCache /> settings to a specific extension in IIS?

查看:13
本文介绍了如何申请 <clientCache/>设置到 IIS 中的特定扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下 Web.config 在 Azure 上托管一个 Web 应用程序:

I am hosting a web app on Azure with the following Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".text" mimeType="text/plain" />
      <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

这可行,但我想通过扩展来改变缓存年龄.我想将 .html 文件的最长使用期限设置为 1 天,将其他所有文件的最长使用期限设置为 365 天.原因是 html 中的所有资产的文件名都在更改时更新,并且从 CDN 提供,因此它们永远不需要过期,但 html 页面本身需要始终保持新鲜,以便用户可以看到新内容.

This works but I would like to vary the cache age by extension. I would like to set the max age of .html files to 1 day and the max age of everything else to 365 days. The reason being that all of the assets in the html have their filenames revved on change and are served from a CDN, so they never need to expire, but the html pages themselves need to always be fresh so the user can see new content.

我看到 <location> 元素允许将 Web.config 过滤到特定位置,但我没有看到将其限制为某些扩展的方法.

I see that the <location> element allows filtering the Web.config to specific locations, but I don't see a way to limit it to certain extensions.

请注意,我不要求能够在 Web.config 中执行此操作:Azure Web App 的任何可能方式都可以.

Please note that I don't require being able to do this in the Web.config: any means possible with an Azure Web App is fine.

推荐答案

正如其他人提到的那样,这是不可能的,所以我想建议一种解决方法来完成这项工作.
您可以通过创建出站规则来替换 html 文件的 Cache-Control 标头来利用 URL 重写模块的功能.

As others have mentioned it is not possible so I would like to suggest a workaround to do the job.
You can take advantage of the capabilities of the URL Rewrite Module by creating an outbound rule to replace Cache-Control header of the html files.

这是配置.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".text" mimeType="text/plain" />
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
        </staticContent>
        <rewrite>
            <outboundRules>
                <rule name="RewriteCacheControlForHTMLFiles" preCondition="FileEndsWithHtml">
                    <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
                    <action type="Rewrite" value="max-age=86400" />
                </rule>
                <preConditions>
                    <preCondition name="FileEndsWithHtml">
                        <add input="{REQUEST_FILENAME}" pattern=".html$" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

我的测试截图:

这篇关于如何申请 &lt;clientCache/&gt;设置到 IIS 中的特定扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆