【问题标题】:Forcing HTTPS requests in node.js server on azurewebsites在 azurewebsites 上的 node.js 服务器中强制使用 HTTPS 请求
【发布时间】:2016-03-11 17:47:59
【问题描述】:

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

我找到了两种方法:

  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 访问。

anyonw 可以提供任何选项吗?

【问题讨论】:

    标签: node.js azure express socket.io azure-web-app-service


    【解决方案1】:

    使用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 中的任何内容。这应该可以工作。

    【讨论】:

      【解决方案2】:

      这适用于我在 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>
      

      【讨论】:

        猜你喜欢
        • 2021-10-10
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 2018-01-28
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        相关资源
        最近更新 更多