【问题标题】:IIS 8.5 cache issue on rewrite if file not exists如果文件不存在,则重写时 IIS 8.5 缓存问题
【发布时间】:2020-07-09 13:13:40
【问题描述】:

我需要在 IIS 中实现以下内容:

  1. 检查 dir 中更改的 .txt 文件,带有 url http://server/dir/test.txt 没有缓存
  2. 如果文件不存在,则将 url 重写为 empty.txt

我的配置:

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

【问题讨论】:

  • 有什么消息吗?好像我有类似的问题。

标签: iis caching url-rewriting


【解决方案1】:

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

因此,如果您想阻止特定文件夹的缓存,请将其放在您的网站/应用程序的根 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" /> 

没有位置属性。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多