2016-03-11 54 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中。這應該只是工作。