2

我想使用web.config重写规则在http Azure应用服务中启用http重定向。根据我所能找到的所有文档,使用重写规则的Web配置重定向是获得此功能的官方方式。我喜欢并希望支持的功能之一是gzip作为Accepted-Encoding,这是默认启用的。但是这两个功能似乎有冲突。是否有另一种方法来做重定向?我必须禁用压缩吗?Azure Http to Https重定向不适用于SPA

这里是我的重定向规则:

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

curl命令与我只想通过SSL进行传输的内容进行响应:

执行如预期
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以外的请求似乎按预期工作。只有root 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> 

回答

0

经过深入挖掘并重新创建问题后,我向微软开了一张票。这个问题是专门针对单页面应用程序和使用重写规则的SSL重定向,当接受压缩内容时。显然,IIS正在缓存gzip响应,所以SSL重定向从不会发生。解决的办法是禁用缓存:

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

这些是我想出了重新创建问题的步骤:

  1. 创建一个新的Azure的应用服务
  2. 重命名hostingstart.html索引。 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. 现在加载未缓存重定向响应的Web浏览器。您将通过http加载站点,而不是重定向到https。

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

根据你的描述,我也跟着你的URL重写规则,并在我的天蓝色的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标题中的响应如下所示:

enter image description here

enter image description here

+0

我能够重新创建该问题。我打开支持票: 1)创建一个天蓝色的网站 2)从我更新的帖子中添加web.config(将index.html更改为该网站的默认文件) 3)在浏览器中加载您的网站将被重定向) 4)加载您的网站在第二个浏览器(你不会被重定向) 5)如果你添加任何东西到网址的末尾,你将被重定向。 –