IIS URL重写反向代理未返回最新数据问题排查请求
解决IIS反向代理返回旧数据的缓存问题
看起来你的问题核心是IIS反向代理层的缓存机制在搞鬼——直接访问后端服务器正常,经过代理就有30秒左右的延迟更新,这几乎肯定是缓存导致的。下面是具体的排查和解决步骤:
1. 禁用IIS针对API路径的输出缓存
IIS默认可能会对动态内容开启缓存,你需要明确禁用/qosapi/路径下的缓存。在你的web.config的<system.webServer>节点下添加以下配置:
<caching> <profiles> <!-- 精准针对qosapi路径禁用缓存 --> <add name="NoCacheForQosApi" path="/qosapi/*" policy="DisableCache" kernelCachePolicy="DisableCache" /> </profiles> </caching>
2. 强制反向代理不缓存响应
在你的反向代理规则里,添加服务器变量来强制禁用缓存,让后端返回的最新内容直接透传。修改ReverseProxyInboundRule1规则,增加<serverVariables>节点:
<rule name="ReverseProxyInboundRule1" stopProcessing="true"> <match url="^qosapi/(.*)" /> <serverVariables> <set name="HTTP_PRAGMA" value="no-cache" /> <set name="HTTP_CACHE_CONTROL" value="no-cache, no-store, must-revalidate" /> </serverVariables> <action type="Rewrite" url="http://172.20.20.23:3000/{R:1}" /> </rule>
3. 检查ARR(Application Request Routing)的缓存设置
如果你安装了ARR模块(IIS反向代理通常依赖它),ARR默认会开启缓存。你可以:
- 打开IIS管理器,找到服务器节点下的Application Request Routing Cache
- 点击Edit Cache Settings,关闭Enable disk cache;或者针对
/qosapi/*路径添加缓存排除规则 - 也可以在
web.config里添加ARR的缓存禁用配置:
<system.webServer> <rewrite> <outboundRules> <rule name="DisableARRCache" preCondition="ResponseIsApiContent"> <match serverVariable="RESPONSE_Cache-Control" pattern=".*" /> <action type="Rewrite" value="no-cache, no-store, must-revalidate" /> </rule> <preConditions> <preCondition name="ResponseIsApiContent"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="application/json|application/xml|text/plain" /> </preCondition> </preConditions> </outboundRules> </rewrite> </system.webServer>
问题根源说明
你遇到的30秒延迟刚好是IIS/ARR默认的缓存过期时长,当你更新后端数据后,代理层还拿着之前缓存的响应返回给客户端,直到缓存过期才会去拉取新数据。而直接访问后端时没有经过这层缓存,所以能拿到最新结果。
内容的提问来源于stack exchange,提问作者sandeep pandey
相关产品推荐
相关产品推荐

