【问题标题】:Forward http to https in Azure在 Azure 中将 http 转发到 https
【发布时间】:2011-10-24 11:10:22
【问题描述】:

我们在 Azure 上部署了一个使用 https 的 Web 角色。由于这是我们希望我们的用户访问系统的唯一方式,我们希望将访问 http 版本的用户转发到 https 版本。

我们已经尝试了建议here

也就是说,我们在 web.config 中添加了以下内容:

<system.webServer>
<rewrite>
  <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
  </rules>
</rewrite>

但是,这似乎不起作用。有谁知道如何做到这一点?这似乎是一个常见的要求......

【问题讨论】:

  • 为了让我的静态角度站点使用 HTTPS,我使用 () 而不是 (.) 并且我有一个重定向到 index.html 的包罗万象,我删除了所有我在应用程序设置下的默认文档。

标签: azure


【解决方案1】:

最简单的方法是安装Redirect HTTP to HTTPS 扩展。

要安装它,请从仪表板打开您的 Web 应用,然后单击“扩展”选项卡。

搜索Redirect HTTP to HTTPS并安装。

就是这样。无需设置或复杂的配置说明

【讨论】:

  • 其他是 IIS 的答案。这就是 Azure 的答案。
【解决方案2】:

添加到这里已经提到的答案。 仔细想想,这很明显,但当我第一次阅读这篇文章时,我却不这么认为。

您必须确保您打开了端口 80 并通过您的 ServiceDefinition 文件将其绑定到您的站点才能正常工作。

示例条目如下所示:

   <Sites>
      <Site name="Web" physicalDirectory=".\SitesRoot">
        <VirtualDirectory name="www" physicalDirectory=".\SitesRoot\dist" />
        <Bindings>
          <Binding name="HttpsEndpoint" endpointName="HttpsEndpoint" />
          <Binding name="HttpEndpoint" endpointName="HttpEndpoint" />
        </Bindings>
      </Site>
    </Sites>

    <Endpoints>
      <InputEndpoint name="HttpsEndpoint" protocol="https" port="443" certificate="MY_SSL_CERT.pfx" />
      <InputEndpoint name="HttpEndpoint" protocol="http" port="80" />
    </Endpoints>

【讨论】:

    【解决方案3】:

    Smarx 刚刚又在几分钟内发表了一篇关于此的博文;)

    http://blog.smarx.com/posts/redirecting-to-https-in-windows-azure-two-methods

    如果网站出现故障,这里是一个摘要:

    IIS URL 重写

    如果您没有使用 ASP.NET MVC,而是使用 IIS(如在 Web 角色中),则可以使用默认安装的 URL Rewrite module。使用这个模块重定向到 HTTPS 相当简单,并且在其他地方有文档记录,但是让一切在本地计算模拟器中正常工作并非易事。特别是,您会发现大多数示例都假定 HTTP 流量将始终在端口 80 上,而在计算模拟器下进行测试时并非总是如此。这是一条似乎在本地和云端都适用的规则:

    <system.webServer>
      <rewrite>
        <rules>
          <rule name="Redirect to HTTPS">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
              <add input="{URL}" pattern="/$" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
          </rule>
        </rules>
      </rewrite>
    

    【讨论】:

    • 我确实做到了。 :-) 我也会添加到该线程中......使用 {SERVER_NAME} 而不是 {HTTP_HOST} 可以改善计算模拟器的体验(因为原始端口很少是 80,所以你最终会得到像 @987654325 这样奇怪的东西@,这不起作用)。
    • 我尝试在配置中添加规则,但它不起作用。这个方法在当前的 azure 环境下还能用吗?
    • 嘿,这是link only answer,你能把重点放在你的答案中,这样如果远程站点出现故障我们就不会丢失它吗?
    • @LukeMcGregor 将要点添加到自己的答案中。
    • 我唯一的评论是,对于本地开发,这个配置仍然不完美,因为它丢失了端口并且无法猜测新端口。我将此规则移至 Web.Release.config 并向其中添加了 xdt:Transform="Insert" 规则。另请注意,上面的示例禁用了根 url 重定向。
    【解决方案4】:

    只是添加到 Kevin Cloet 对 Smarx 博客的回答。要仅在 Release 模式下设置 RequireHttps 属性, 你可以使用 HttpContext.Current.IsDebuggingEnabled

        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
    
            if (!HttpContext.Current.IsDebuggingEnabled) {
                filters.Add(new RequireHttpsAttribute());
            }
        }
    

    请参考此链接: https://stackoverflow.com/a/27324439/1933168

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 2021-05-04
      • 2016-05-04
      • 2015-05-24
      • 2012-08-24
      • 2018-09-05
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多