http 到 https 重写太多重定向循环 IIS 7

http to https rewrite too many redirect loops IIS 7

我有一个托管在 IIS 7.0 中的应用程序。 我必须确保它只适用于 HTTPS 而不是 HTTP 所以我在我的根配置中包含了以下规则。

<rewrite>
        <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"   redirectType="Found" />
            </rule>
        </rules>
</rewrite> 

添加此规则后,当我尝试访问我的应用程序时出现以下错误:

Page has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. Here are some suggestions: Reload this web page later. Learn more about this problem.

输入条件如下:

<add input="{HTTPS}" pattern="on" /> 

而不是:

<add input="{HTTPS}" pattern="off" />

我们的 ASP.NET 应用程序托管在带有 Elastic Load Balancing 的 AWS 上,问题中接受答案的规则对我们不起作用,并不断导致无限重定向。

这是最终对我们有用的规则:

<rewrite>
   <rules>
      <rule name="HTTPS Rule behind AWS Elastic Load Balancer Rule" stopProcessing="true">
         <match url="^(.*)$" ignoreCase="false" />
         <conditions>
            <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />
         </conditions>
         <action type="Redirect" url="https://{SERVER_NAME}{URL}" redirectType="Found" />
      </rule>
   </rules>
</rewrite>

此外,正如 SNag 所提到的,我们有一个站点位于亚马逊的 ELB 后面。尝试在没有以下输入 header 的情况下应用重写规则会导致无限重定向。这似乎是需要输入类型 HTTP_X_FORWARDED_PROTO 的结果,如下所示:<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />

来自 AWS 文档 "Your application or website can use the protocol stored in the X-Forwarded-Proto request header to render a response that redirects to the appropriate URL." 我们正在使用带有 DNS 条目的 ELB 转发到服务器上的站点。

我的情况,我需要这样写:

<rewrite>
<rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" />
        <add input="{HTTPS}" pattern="on" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"   redirectType="Found" />
    </rule>
</rules>

对于 IIS 10(Windows Server 2016)我遵循了 here 的说明,它为重写生成了一个略有不同的 XML 配置:

<rewrite>
    <rules>
        <rule name="HTTP 2 HTTPS" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
        </rule>
    </rules>
</rewrite>

模式为off,匹配仅为*

我正在使用 Liquid Web Cloud Sites,并且 运行 遇到了完全相同的问题。

我在这里尝试了解决方案,但由于这种情况,它无法满足我的需要:

<add input="{HTTPS}" pattern="off" />

正如 OP 所说,这意味着,“在 HTTPS 关闭时匹配并实施此规则”。而这个问题的公认解决方案只是颠倒了这个,并且在HTTPS 开启 时匹配规则。它解决了无限循环问题,但这只是因为我的规则匹配不正确——我实际上只想在 HTTPS 关闭时将请求更改为 HTTPS。因此 none 我的 HTTP 请求被转发了。

有趣的是,我的 HTTPS 请求中的 none 也被转发了,从这个(以及我所做的其他一些测试)我确定虽然浏览器显示 HTTPS,但服务器将其视为 HTTP要求。因此,服务器始终认为它正在接收 HTTP 请求,并且始终忽略该规则(现在指定仅匹配启用 HTTPS 的请求 - 即从不)。

经过几个小时的研究和测试,我推断这是一个与 described here 类似的问题,总结如下:

To reduce costs [many hosting providers install the] SSL certificate on the TMG Gateway and this gateway is simply rewriting the request to standard HTTP when passing it to the actual web server. So by the time the request hits IIS and your web application it is a standard plain HTTP request.

.

TLDR;

最后,我与 Liquid Web 的团队进行了交谈,他们向我指出了埋藏在他们自己网站中的帮助文章的方向,该文章解决了该问题。他们建议我使用以下修复它的重写规则:

<system.webServer>
 <rewrite>
  <rules>
   <rule name="Redirect to HTTPS" stopProcessing="true">
     <match url=".*"/>
    <conditions>
     <add input="{HTTP_CLUSTER_HTTPS}" pattern="^on$" negate="true"/>
     <add input="{HTTP_CLUSTER_HTTPS}" pattern=".+" negate="true"/>
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{SCRIPT_NAME}" redirectType="SeeOther"/>
   </rule>
  </rules>
 </rewrite>
</system.webServer>

我希望这对处于类似情况的其他人有用。

Original liquidweb help article

我也遇到过这个问题。对服务器的所有请求都是 HTTP。 就我而言,问题是我使用 Cloudflare DNS。 有一个 SSL/TLS 设置,默认情况下 SSL/TLS 加密模式设置为灵活。

确保将模式更改为完整。

关于这个问题我想通了。基本上,如果传入请求是 HTTPS,则什么都不做。

          <rule name="No Redirect if https" enabled="true" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
                    <add input="{HTTPS}" pattern="^ON$" />
                </conditions>
                <action type="None" />
            </rule>
            <rule name="Redirect to https" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="^\example\.com$" />
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{R:0}" />
            </rule>

如果您将 cloudflare 用于 SSL,请将其置于完整模式

Cloudflare -> SSL/TLS -> 概述 -> 完整

Figure