【问题标题】:HTTPS redirect works locally but not on azureHTTPS 重定向在本地有效,但在 azure 上无效
【发布时间】:2016-12-19 07:44:43
【问题描述】:

这个问题可能被问了一千遍,但我仍然找不到解决方案..

我有一个 .NET 应用程序,它在本地运行时会从 HTTP 重定向到 HTTPS。但是在 Azure Web App 中部署时,重定向将不起作用。

基于多个论坛帖子,我的重定向是使用Web.config 文件中的规则完成的。下面,您会看到此文件部署在 azure 上(我使用 ftp 检查过)

  <system.webServer>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
          </conditions>
          <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="static dist files" stopProcessing="true">
          <match url="^(.+)$" />
          <conditions>
            <add input="{APPL_PHYSICAL_PATH}dist\{R:1}" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="/dist/{R:1}" />
        </rule>
        <rule name="index.html as document root" stopProcessing="true">
          <match url="^$" />
          <action type="Rewrite" url="/dist/index.html" />
        </rule>
      </rules>
      <!--<outboundRules>
      <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
        <match serverVariable="RESPONSE_Strict_Transport_Security"
            pattern=".*" />
        <conditions>
          <add input="{HTTPS}" pattern="on" ignoreCase="true" />
        </conditions>
        <action type="Rewrite" value="max-age=31536000" />
      </rule>
    </outboundRules>-->
    </rewrite>
  </system.webServer>

这个Web.config文件是对原始Web.config文件转换的结果,因为在本地,HTTPS的端口设置为44362,因此本地使用的Web.config是有点不同:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="([^/:]*):[^/]*?" />
      </conditions>
      <action type="Redirect" url="https://{C:1}:44362/{R:1}"
          redirectType="Permanent" />
    </rule>

在本地,一切正常。重定向在瞬间完成。 但是,当部署在 azure Web 应用程序中时,不会发生重定向。 所以“---.azurewebsites.net”不会自动重定向到“https://---.azurewebsites.net

但我可以看到,他达到了 te 规则,因为浏览器选项卡的标题瞬间以该规则的名义重命名。

如果有人发现此代码中的愚蠢错误,请告诉我。

问候,

【问题讨论】:

    标签: .net azure ssl https azure-web-app-service


    【解决方案1】:

    Azure 应用服务中有一个可用的扩展程序可以自动为您执行此操作。

    这些方向可能会随着 Azure 门户的变化而变化,但它是 2018-05 的最新版本。

    1. 在 Azure 门户中转到您的应用服务。
    2. 点击顶部菜单中的“工具”。
    3. 在“开发”部分,点击“扩展”。
    4. 在“扩展”边栏选项卡中,单击顶部的“添加”。
    5. 在“选择扩展”边栏选项卡中,选择“将 HTTP 重定向到 HTTPS”。
    6. 单击“确定”或其他任何内容,直到保存为止。

    之后,您的所有 HTTP 流量都将重定向到 HTTPS,无需进行其他更改。

    【讨论】:

      【解决方案2】:

      我用过

       <rewrite>
        <rules>
          <rule name="Redirect to https">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="Off" />
              <add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
          </rule>
        </rules>
      </rewrite>
      

      在 Azure 中相当成功

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 2022-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-08
        相关资源
        最近更新 更多