首页 > 解决方案 > 如果文件不存在,则重写时 IIS 8.5 缓存问题

问题描述

我需要在 IIS 中实现以下目标:

  1. 使用没有缓存的 url检查更改的.txt文件dirhttp://server/dir/test.txt
  2. empty.txt如果文件不存在,则重写 url

我的配置:

<rewrite>
  <rules>
    <rule name="test-txt" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*.txt" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Rewrite" url="empty.txt" />
    </rule>
  </rules>
</rewrite>
<caching>
  <profiles>
    <add extension=".txt" policy="DisableCache" kernelCachePolicy="DisableCache" />
  </profiles>
</caching>

如果文件被更改,我会得到代码 200 和实际内容。按预期工作。

如果文件未更改 - 304。按预期工作。

如果文件被删除,在第一次检查后我得到错误 404.0。为什么?

在第二次请求后,我得到代码 200 和empty.txt. 按预期工作。

如果我当时创建文件,它会按预期工作。但是,如果在很短的时间间隔内(每 3-4 秒)发出请求,则文件是否存在都无关紧要,如果我更改它test.txt,我会得到代码 304 或内容。empty.txt但是,如果我停止请求文件几分钟,它会再次按预期工作。

看起来 IIS 具有某种用于静态文件检查的缓存。如何仅使用静态文件使其按预期工作?(如果可能的话,不会出现错误 404)

标签: iiscachingurl-rewriting

解决方案


它不是输出缓存,它只是关于浏览器缓存。

因此,如果您想阻止特定文件夹的缓存,请将其放在您的网站/应用程序的根 web.config 中。在这种情况下,您必须禁用 example.txt 和 dir 目录的位置路径

  <location path="dir">
        <system.webServer>
            <staticContent>
                <clientCache cacheControlMode="DisableCache" />
            </staticContent>
        </system.webServer>
    </location>
<location path="empty.txt">
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="DisableCache" />
        </staticContent>
    </system.webServer>
</location>

如果要禁用所有站点的浏览器缓存,请设置

  <clientCache cacheControlMode="DisableCache" /> 

没有位置属性。

您的规则在我这边运行良好,因此您只需为所需的文件夹/文件禁用客户端缓存。

在此处输入图像描述

在此处输入图像描述


推荐阅读