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

Mic*_*ski 9 azure azure-web-sites azure-web-app-service

我使用Azure云与Web应用程序和我的服务器端写入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>
Run Code Online (Sandbox Code Playgroud)

问题是当我输入浏览器" https://myURL.com "时,它会重定向到主屏幕,但是当我将https更改为http" http://myURL.com "时,它会重定向到https:// myURL.com/ "并添加到网址"bin/www",根据网址看起来像" http://myURL.com/bin/www ",响应是:页面找不到.

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

我的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>
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答,迈克尔.

brc*_*-dd 32

转到 Azure 门户并打开要设置为仅 HTTPS 的 (Web) 应用服务的概述页面。在侧边栏中的Settings部分下,有一个TLS/SSL Settings选项。

单击它后,您将在屏幕上看到一个选项,用于将您的应用程序协议设置为HTTPS only。无需为此手动添加单独的规则集。

这适用于应用服务计划的每一层,包括“F”系列(免费订阅)。

TLS/SSL 设置页面

请注意,如果您要添加任何自定义域,您还需要添加相应的 SSL 绑定,您可以使用 LetsEncrypt 或类似方法轻松获取它们。如果您的应用程序的任何自定义主机名缺少 SSL 绑定,则:

当启用 HTTPS Only 时,在这些自定义主机名上访问您的应用程序的客户端将看到安全警告。

PS:我刚刚看到这个问题是在大约 3 年前被问到的,那个时候也许没有直接的选择来做到这一点。但即便如此,我还是要发布我的答案,因为截至 2020 年 2 月,在 Google 上,这个问题在 Azure 中的自动 HTTPS 重定向方面仍然排名第一。


Joh*_*son 10

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

  • 2023 年 4 月,位于配置 -&gt; 常规设置下 (3认同)

pgm*_*ank 6

此外还有一个免费的开源扩展.

  1. 转到Web App设置侧栏,搜索"Extensions"选项卡,然后单击"Add".

扩展选项卡

  1. 向下滚动,找到扩展重定向HTTP到HTTPSgregjhogan.

添加扩展名

  1. 接受条款.

接受条款

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

  2. 做完了!

有关此扩展的实现的更多详细信息,请检查GitHub上的源代码.最重要的源文件是applicationhost.xdt.

报价从GitHub(2017年2月8日)(学分去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>
Run Code Online (Sandbox Code Playgroud)

  • 此扩展在 Azure 中不再可用。您现在可以转到“设置”-&gt;“TLS/SSL 设置”并将“仅 HTTPS”设置为“开”。 (2认同)

小智 3

R:1 是对规则模式的反向引用。您将其附加到此处的网址:

url="https://{HTTP_HOST}/{R:1}" 
Run Code Online (Sandbox Code Playgroud)

把它改成

url="https://{HTTP_HOST}" 
Run Code Online (Sandbox Code Playgroud)

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