2013-12-14 55 views
3

在Windows Azure網站上運行,我想通過默認的* .azurewebsites.net證書使用ssl。它的工作原理沒有做任何事情,但http也可用於每個目的地,而不僅僅是https。我如何強制從http重定向到https?通常情況下我可以這樣做:你如何強制在Azure網站的node.js上使用https?

var https = require('https'); 

... 

var options = { 
     key: fs.readFileSync('path.key'), 
     cert: fs.readFileSync('path.crt') 
    }; 

... 

https.createServer(options, app) 

,但因爲我不知道的* .azurewebsites.net證書,如它的路徑,這是行不通的任何東西。

如何將所有或某些請求重定向到https?

回答

6

web.config,有stopProcessing="true"任何其他規則之前添加下面的規則。

<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> 

你也可以使用正常http.createServer(app)進行生產,如果你想將* .azurewebsite.net通配符證書。

參考文獻:

  1. How to require SSL in IIS7 and Azure with Rewrite
  2. URL Rewrite Module Configuration Reference
+0

這是行得通的,但在瀏覽器上打開app.js文件。有任何想法嗎? – newbie

0

因爲它聽起來像Azure的網站充當你的情況下,反向代理,這種計算策略可能會爲你工作:

如果你可以從下面的協議,它應該幫助你:

req.headers['x-forwarded-proto'] 

這應該爲您提供您需要鍵入的http或https,以便在您服務的資源無效時執行重定向。

我使用類似下面的代碼重定向任何時候我收到一個請求(例如一個HTML文件或網站的根)。我把所有我想成爲一個/安全目錄安全的文件,可以很容易地知道什麼應該和不應該是SSL:

protocol = req.headers['x-forwarded-proto']; 

if ((req.url.lastIndexOf('.html') == (req.url.length - 5)) || (req.url.slice(-1) == '/')) { 
    if (protocol == 'http') { 

     if (req.url.indexOf('/secure/') == 0) { 
      console.log('Non ssl request made to secure resource: ' + req.url); 
      console.log('Redirecting to https://' + site_host_name + req.url); 
      res.writeHead(301, 
       {Location: 'https://' + site_host_name + req.url} 
      ); 
      res.end(); 
      return; 
     } else { 
      next(); 
      return; 
     } 
    } else { 
     if (req.url.indexOf('/secure/') != 0) { 
      console.log('ssl request made for non-secure resource: ' + req.url); 
      console.log('Redirecting to http://' + site_host_name + req.url); 
      res.writeHead(301, 
       {Location: 'http://' + site_host_name + req.url} 
      ); 
      res.end(); 
      return; 
     } else { 
      next(); 
      return; 
     } 
    } 
} 
1

如果你喜歡,以驗證您的節點的應用程序從節點的應用程序中接收安全流量,理查德·阿斯特伯裏有solution on his blog

tl; dr:IIS充當節點應用程序的代理,並且在請求通過HTTPS提供服務時將「x-arr-ssl」頭添加到請求中。您還可以使用「x-site-deployment-id」標題來驗證您的應用程序當前是否正在以azure運行。中間件結束了,像這樣:

function requireHTTPS(req, res, next) { 
    var isAzure = req.get('x-site-deployment-id'), 
     isSsl = req.get('x-arr-ssl'); 

    if (isAzure && !isSsl) { 
     return res.redirect('https://' + req.get('host') + req.url); 
    } 

    next(); 
} 

app.use(requireHTTPS); 

當你在它,它添加HSTS是一個好主意,太:

var helmet = require('helmet'); 

function requireHTTPS(req, res, next) { 
    var isAzure = req.get('x-site-deployment-id'), 
     isSsl = req.get('x-arr-ssl'); 

    if (isAzure && !isSsl) { 
     return res.redirect('https://' + req.get('host') + req.url); 
    } 

    next(); 
} 

app.use(requireHTTPS); 
app.use(helmet.hsts({ 
    maxAge: 10886400000,  // Must be at least 18 weeks to be approved by Google 
    includeSubdomains: true, // Must be enabled to be approved by Google 
    preload: true 
})); 

當然,你可以隨時與web做到這一點串聯.config回答。

4

接受的答案是不工作對我來說,它只是顯示一個空白頁面,這一個:

<!-- Redirect all traffic to SSL --> 
    <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> 

https://gist.github.com/tstpierre/afec7eb409aebe0bf3d1作品完美,但。

+0

這有效,但它會在瀏覽器上打開我的app.js文件。有任何想法嗎? – newbie

相關問題