在 azurewebsites 的 node.js 服务器中强制执行 HTTPS 请求

Forcing HTTPS requests in node.js server on azurewebsites

我在 Azure 网站的 IIS 中部署了 node.js 服务器,我想阻止或重定向 http 请求。我在我的服务器中使用 express + socket.io。

我找到了 2 种方法:

  1. 在实际 socket.io 代码中由 passing allowRequest parameter 变为 socket.io。所以我的代码看起来像这样:

    var checkRequest = function (req, fn) {
           fn(err, true);
    };

var ioOptions = {
            pingInterval: socketPingInterval,
            pingTimeout: socketPingTimeout,
            path: "/" + config.API_VERSION + "/socket.io",  
            allowRequest : checkRequest
    };

    _socketListener = io.listen(server, ioOptions);

问题是代码永远不会进入 checkRequest 方法,我不知道为什么。

  1. 将规则添加到 web.config 文件。我检查了几个论坛,每个人都说如果我添加这段代码:

<rule name="RedirecttoHTTPS">
        <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>

它将我的 hpt 请求重定向到 HTTPS。但它仍然有效,我可以通过 HTTP 访问。

任何人都可以帮忙选择吗?

使用 Kudu Console,在您的 d:\home\site 文件夹中创建一个 applicationhost.xdt 文件,其中包含以下内容:

  <rewrite xdt:Transform="InsertIfMissing">
    <rules xdt:Transform="InsertIfMissing">
      <rule name="Force HTTPS" enabled="true" stopProcessing="true">
        <match url="(.*)" ignoreCase="false" />
        <conditions>
          <add input="{HTTPS}" pattern="off" />
          <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>

并删除您添加到 web.config 中的所有内容。这应该可以正常工作。

这对我在 Azure 中的 Node Web 应用程序很有用...

https://stpdev.wordpress.com/2015/09/23/force-https-redirection-for-nodejs-apps-hosted-in-azure/

<rewrite>
    <rules>
        <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="true" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>