Azure Web应用程序将http重定向到https

时间:2016-08-31 08:18:41

标签: azure azure-web-sites azure-web-app-service

我在网络应用中使用Azure云,在nodejs上使用我的服务器端。 当Web应用程序收到http请求时,我想将请求重定向到https 我找到了解决方案。 我将它放在rules标记

中的web.config文件中
        <rule name="Force HTTPS" enabled="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
        </rule>

问题是当我在浏览器中输入&#34; https://myURL.com&#34;每件事都可以重定向到主屏幕, 但当我将https更改为http&#34; http://myURL.com&#34;它重定向到https://myURL.com/&#34;并添加到网址&#34; bin / www&#34;根据该网址看起来像&#34; http://myURL.com/bin/www&#34;,回复是:页面找不到。

问题是如何将没有添加数据的明确网址重定向到网址?

我的web.config文件的一部分:

<rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^bin/www\/debug[\/]?" />
        </rule>
        <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}" />
        </rule>
        <!-- All other URLs are mapped to the node.js site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
          </conditions>
          <action type="Rewrite" url="bin/www" />
        </rule>
        <!-- Redirect all traffic to SSL -->
         <rule name="Force HTTPS" enabled="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>

谢谢你的回答,Michael。

4 个答案:

答案 0 :(得分:16)

转到Azure门户并打开您想要设置为仅HTTPS的(Web)应用程序服务的概述页面。在样式部分下方的侧栏中,有一个TLS / SSL设置选项。单击它后,您将在屏幕上看到一个选项,以设置为“仅HTTPS”。无需为此手动添加单独的规则集。此功能适用于应用服务计划的每个层,包括“ F”系列(免费订阅)。

SSL/TLS Settings Page

PS:我刚刚看到这个问题是3年前问的,那个时候也许没有直接的选择。但是我仍在发布我的答案,因为截至2020年2月,该问题在有关HTTPS自动重定向的各种问题中仍排在第一位,认为这将对新查看者有所帮助。

答案 1 :(得分:6)

此外还有一个免费的开源扩展程序。

  1. 转到您的网络应用设置侧边栏,搜索“扩展程序”标签,然后点击“添加”。
  2. Extension Tab

    1. 向下滚动,找到 gregjhogan 扩展程序将HTTP重定向到HTTPS
    2. Add Extension

      1. 接受条款。
      2. Accept Terms

        1. 重新启动Web App以使操作立即生效。

        2. 完成!

        3. 有关此扩展程序实施的更多详细信息,请check the source code on GitHub。最重要的源文件是applicationhost.xdt

          引用from GitHub 02-08-2017 )(积分转到 gregjhogan ):

            

          <强> applicationhost.xdt

          <?xml version="1.0"?>
          <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
              <location path="%XDT_SITENAME%" xdt:Transform="InsertIfMissing" xdt:Locator="Match(path)">
                  <system.webServer xdt:Transform="InsertIfMissing">
                      <rewrite xdt:Transform="InsertIfMissing">
                          <rules xdt:Transform="InsertIfMissing" lockElements="clear">
                              <rule name="redirect HTTP to HTTPS" enabled="true" stopProcessing="true" lockItem="true">
                                  <match url="(.*)" />
                                  <conditions>
                                      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                                      <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
                                  </conditions>
                                  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
                              </rule>
                          </rules>
                      </rewrite>
                  </system.webServer>
              </location>
          </configuration>
          

答案 2 :(得分:3)

R:1是规则模式的back-reference。您可以将其附加到此处的网址:

url="https://{HTTP_HOST}/{R:1}" 

将其改为

url="https://{HTTP_HOST}" 

应导致重定向到https根目录。

答案 3 :(得分:2)

截至2017年11月,现在这是Azure门户中的一个简单切换:“仅限HTTPS”,位于自定义域下。

https://blogs.msdn.microsoft.com/benjaminperkins/2017/11/30/how-to-make-an-azure-app-service-https-only/

在ARM中也很容易: “httpsOnly”: true

相关问题