如何将 <clientCache /> 设置应用于 IIS 中的特定扩展?
How can I apply <clientCache /> settings to a specific extension in IIS?
我在 Azure 上托管一个 Web 应用程序 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 页面本身需要始终保持新鲜,因此用户可以看到新内容。
我看到 <location>
元素允许将 Web.config 过滤到特定位置,但我没有看到将其限制为某些扩展名的方法。
请注意,我要求能够在Web.config中执行此操作:使用 Azure Web 应用程序的任何可能方式都可以。
这些文件(html 与内容)是否在不同的文件夹中?
如果是这样,您可以设置一个文件夹特定的缓存年龄
<configuration>
<location path="[html files path]">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="[your requirement]" ></clientCache>
</staticContent>
</system.webServer>
</location>
</configuration>
很遗憾,<location>
元素不支持 regex or wildcards,因此您将无法完全按照您的要求进行操作。
不过,我可以想出几种方法来实现您的目标。第一个是 write an http module 拦截对图像的请求并更改缓存 headers。
另一种选择是保持 html 不变,但将所有图像移动到一个图像文件夹中。然后您可以在该文件夹上设置缓存规则。您可以使用重定向规则将图像请求重定向到该图像文件夹。如果您选择此路径,我可以为您提供有关如何设置它的更多信息。
正如其他人提到的那样,这是不可能的,所以我想建议一个解决方法来完成这项工作。
您可以通过创建出站规则来替换 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>
我的测试截图:
我在 Azure 上托管一个 Web 应用程序 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 页面本身需要始终保持新鲜,因此用户可以看到新内容。
我看到 <location>
元素允许将 Web.config 过滤到特定位置,但我没有看到将其限制为某些扩展名的方法。
请注意,我要求能够在Web.config中执行此操作:使用 Azure Web 应用程序的任何可能方式都可以。
这些文件(html 与内容)是否在不同的文件夹中? 如果是这样,您可以设置一个文件夹特定的缓存年龄
<configuration>
<location path="[html files path]">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="[your requirement]" ></clientCache>
</staticContent>
</system.webServer>
</location>
</configuration>
很遗憾,<location>
元素不支持 regex or wildcards,因此您将无法完全按照您的要求进行操作。
不过,我可以想出几种方法来实现您的目标。第一个是 write an http module 拦截对图像的请求并更改缓存 headers。
另一种选择是保持 html 不变,但将所有图像移动到一个图像文件夹中。然后您可以在该文件夹上设置缓存规则。您可以使用重定向规则将图像请求重定向到该图像文件夹。如果您选择此路径,我可以为您提供有关如何设置它的更多信息。
正如其他人提到的那样,这是不可能的,所以我想建议一个解决方法来完成这项工作。
您可以通过创建出站规则来替换 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>
我的测试截图: