【问题标题】:Azure Http to Https redirect not working for SPAAzure Http 到 Https 重定向不适用于 SPA
【发布时间】:2017-08-29 19:28:08
【问题描述】:

我正在尝试使用 web.config 重写规则在我们的 Azure 应用服务中启用 http 到 https 重定向。根据我能找到的所有文档,使用重写规则的 Web 配置重定向是获得此功能的官方方式。我喜欢并希望支持的功能之一是 gzip 作为 Accepted-Encoding,这是默认启用的。但这两个特征似乎有冲突。还有另一种方法来进行重定向吗?我必须禁用压缩吗?

这是我的重定向规则:

<rule name="Redirect to https" stopProcessing="true">
    <match url="(.*)"/>
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="Off"/>
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>

使用我只想通过 SSL 传递的内容响应的 Curl 命令:

curl "http://[MyUrl]/" -H "Accept-Encoding: gzip, deflate" --compressed

按预期执行的Curl命令:

curl "http://[MyUrl]/"

回复:

HTTP/1.1 301 Moved Permanently
Content-Length: 154
Content-Type: text/html; charset=UTF-8
Location: https://[MyUrl]/
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Date: Tue, 29 Aug 2017 19:29:29 GMT

提前致谢。

如果这很有价值,根 url 之外的请求似乎可以按预期工作。只有根 url 似乎返回没有重定向的内容。

更新
我认为这可能与我的 SPA 重写规则有关,尽管我不确定干扰,因为第一条规则对其进行了 stopProcessing。我能够在 cdwredirecttest.azurewebsites.net 上重新创建该问题。如果您点击该站点,它会将您重定向到 https,但只是第一次。打开另一个浏览器并重试,这一次它将通过 http 加载站点。在服务器缓存 gzip 响应后,您将不再被重定向。这是我的完整 web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect to https" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{HTTPS}" pattern="Off" ignoreCase="true"/>
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
                </rule>
                <rule name="Redirect all requests">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="index.html" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

【问题讨论】:

    标签: azure ssl iis single-page-application azure-web-app-service


    【解决方案1】:

    根据您的描述,我遵循了您的 URL 重写规则并在我的 azure Web 应用程序上进行了测试。我指定了-L 选项,以使curl 遵循HTTP 重定向,如下所示:

    curl "http://{your-webapp}.azurewebsites.net/" -L -H "Accept-Encoding: gzip, deflate" --compressed -v

    curl "http://{your-webapp}.azurewebsites.net/home/index" -L -H "Accept-Encoding: gzip, deflate" --compressed -v

    我可以从上述命令中检索到带有Content-Encoding: gzip 标头的响应,如下所示:

    【讨论】:

    • 我能够重现该问题。我正在打开支持票:1)创建一个 azure 站点 2)从我更新的帖子中添加 web.config(将 index.html 更改为站点的默认文件) 3)在浏览器中加载您的站点(您将被重定向) 4) 在第二个浏览器中加载您的网站(您不会被重定向) 5) 如果您在 url 的末尾添加任何内容,您将被重定向。
    【解决方案2】:

    在挖掘并重新创建问题后,我向 Microsoft 开了一张票。该问题专门针对单页应用程序和在接受压缩内容时使用重写规则进行 SSL 重定向。显然 IIS 正在缓存 gzip 响应,因此 SSL 重定向永远不会发生。解决方案是禁用缓存:

    <system.webServer>
        ...
        <caching enabled="false" enableKernelCache="false"/>
        ...
    </system.webServer>
    

    这些是我想出的重现问题的步骤:

    1. 创建新的 Azure 应用服务
    2. 将 hostingstart.html 重命名为 index.html(可能没有必要,除非重写规则稍后使用它)
    3. 添加以下 web.config

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          <system.webServer>
              <rewrite>
                  <rules>
                      <rule name="Redirect to HTTPS" stopProcessing="true">
                          <match url="^(.*)$" ignoreCase="true" />
                          <conditions logicalGrouping="MatchAny">
                              <add input="{HTTPS}" pattern="Off" ignoreCase="true"/>
                          </conditions>
                          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
                      </rule>
                      <rule name="Single Page App">
                          <match url="^(.*)$" ignoreCase="false" />
                          <conditions logicalGrouping="MatchAll">
                              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                          </conditions>
                          <action type="Rewrite" url="index.html" appendQueryString="true" />
                      </rule>
                  </rules>
              </rewrite>
          </system.webServer>
      </configuration>
      
    4. 在 curl 中加载 http 站点。这将导致重定向响应按预期发生。

      curl "http://cdwredirecttest.azurewebsites.net" -H "Accept-Encoding: gzip, deflate" --compressed
      
    5. 在 curl 中加载 https 站点。您将按预期获得页面内容(我相信 IIS 正在缓存此 gzip 响应并稍后使用它)。

      curl "https://cdwredirecttest.azurewebsites.net" -H "Accept-Encoding: gzip, deflate" --compressed
      
    6. 在 curl 中加载 http 站点。如果收到重定向响应,则需要重复 5 和 6。最终响应将返回内容。

      curl "http://cdwredirecttest.azurewebsites.net" -H "Accept-Encoding: gzip, deflate" --compressed
      
    7. 现在加载一个没有缓存重定向响应的网络浏览器。您将通过 http 加载网站,而不是重定向到 https。

    8. 只要 IIS 放弃缓存响应,您就可以使用 curl 查看重定向,并且可以重复 5 和 6 以使应用再次进入失败状态。

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 1970-01-01
      • 2020-04-29
      • 2014-07-31
      • 1970-01-01
      • 2019-08-03
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多