在不使用缓存不安全服务器变量的情况下在 IIS 中强制执行 HTTPS

Enforce HTTPS in IIS without using cache unsafe server variables

我目前正在使用以下规则在网站上强制执行 https:

<rule name="Enforce HTTPS" enabled="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" ignoreCase="true" pattern="^off$" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

问题是我在条件中使用的 HTTPS 服务器变量不是“不会对输出缓存策略造成任何影响”的服务器变量列表的一部分 (see full list)。

之所以出现这个问题,是因为在设置了一些失败请求跟踪规则后,我意识到某些条目有以下警告:REWRITE_DISABLED_KERNEL_CACHE

经过一番研究,我发现了一个相关的 forum thread 提到:

The URL Rewrite Module will disable kernel mode cache if any rule in a rule set had a condition that used cache unsafe server variable. The cache was disabled regardless whether the requested URL matched the rule pattern or not.

如果性能不是问题,这就不是问题,但站点速度不是那么快,我想知道这个缓存问题是否不是问题的一部分。

从阅读安全变量列表来看,似乎有 none 将包含用于请求的协议,所以这似乎不可能,但我仍然认为我可能会问案例.

我们可以绕过HTTP协议使用HTTPS访问网站,然后通过IIS日志查看网站的访问记录,其中每条记录都包含访问的时间消耗。

Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs- 
version cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken  

2020-04-15 02:50:57 fe80::a4fe:6d79:f2b8:d031%6 GET / - 446 - 
fe80::a4fe:6d79:f2b8:d031%6 HTTP/2 Mozilla/5.0+ 
(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+ 
(KHTML,+like+Gecko)+Chrome/80.0.3987.163+Safari/537.36
- 403 14 0 2158

最后一个字段表示一个请求中的time-taken。
我认为仅检查HTTPS服务器变量并重定向URI时,IIS输出缓存不会对IIS网站访问性能产生重大影响。

有同样的问题并通过将 HTTP 流量路由到一个单独的 IIS 应用程序来解决这个问题,该应用程序只包含一个 web.config 文件,该文件将流量路由到 HTTPS 应用程序。因此 HTTPS 应用程序中的 web.config 文件没有任何不安全的变量。