2016-03-11 52 views
0

我在azure网站上的IIS中部署了node.js服务器,并且我想阻止或重定向http请求。我在我的服务器中使用express + socket.io。在azurewebsites上的node.js服务器中强制HTTPS请求

我发现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访问。

can anyonw help with any option?

回答

2

使用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中。这应该只是工作。