azure - 如何将 <clientCache/> 设置应用于 IIS 中的特定扩展?

标签 azure iis web-config azure-web-app-service

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

<?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 页面本身需要始终保持新鲜状态,以便用户可以看到新内容。

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

请注意,我不要求能够在 Web.config 中执行此操作:使用 Azure Web 应用程序的任何可能方法都可以。

最佳答案

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

这是配置。

<?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>

我的测试截图:

enter image description here

关于azure - 如何将 <clientCache/> 设置应用于 IIS 中的特定扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36783181/

相关文章:

ASP.NET:HTTP 错误 500.19 – 内部服务器错误 0x8007000d

azure - 每个实例的 Web 套接字和 IP 连接之间有什么区别?

Azure 表存储 - 501 未实现

c# - Azure C# v2 函数 : Entire table processed instead of portion of table

amazon-web-services - 通过应用程序负载平衡和 MSSQL AlwaysOn 将应用程序托管在 AWS 中

IIS 7 URL 重写 - 403 错误

c# - ASP.NET 中无法解释的 session 超时

sql-server - SQL Server不存在或访问被拒绝-适用于SQL Server Express

azure - 您能否建议如何解决 azure Web 应用程序的 URL 重写规则问题?

web-config - <defaultProxy> 的 appsetting.json 版本是什么?