2013-02-26 26 views
1

根據標題,我有一個Node.js應用程序,我希望能夠檢測是否通過HTTPS或HTTP進行請求。到目前爲止,我重定向看起來是這樣的:如何檢測Azure網站上的HTTPS重定向?

// Ensure the page is secure, or that we are running a development build 
if (req.headers['x-forwarded-proto'] === 'https' || process.env.NODE_ENV === 'development') { 
    res.render('index'); 
} else { 
    winston.info('Request for login page made over HTTP, redirecting to HTTPS'); 
    res.redirect('https://' + req.host); 
} 

其中一期工程於Nodejitsu罰款,但重定向HTTPS請求不具有的「X - 轉發,原」頭Azure上的設置。

+0

它看起來像我可以使用 「X-ARR-ssl的」 頭的。 – Siyfion 2013-02-26 09:34:28

+1

根據我的理解,頭文件「x-forwarded-proto」來自某個實際的客戶端。雖然重定向一些間歇性運行的應用程序,但這些重定向不包括x-forward-proto頭文件。我覺得使用其他參數來識別協議而不是標題信息會更好。 – 2013-02-26 14:02:00

+0

你會建議使用什麼? Azure和Nodejitsu都使用代理將HTTPS流量重定向到HTTP服務器。沒有什麼我可以使用的嗎? – Siyfion 2013-02-26 15:03:37

回答

6

我想我是在我的意見正確:

X-ARR-SSL似乎是檢查頭。

// Ensure the page is secure, or that we are running a development build 
if (req.headers['x-forwarded-proto'] === 'https' || req.headers['x-arr-ssl'] || process.env.NODE_ENV === 'development') { 
    res.render('index'); 
} else { 
    winston.info('Request for login page made over HTTP, redirecting to HTTPS'); 
    res.redirect('https://' + req.host); 
} 
4

進入完全相同的問題,但以不同的方式解決它。如果您使用Express並啓用信任代理,則req.protocol將選取x-forwarded-proto標頭。 Azure不會設置x-forwarded-proto標頭,但是您可以使用x-arr-ssl標頭手動對其進行破解,以便req.protocol將在您的應用程序的其餘部分中返回正確的值。

這裏有一個要點:https://gist.github.com/freshlogic/6348417

var express = require('express'); 

var app = express(); 
app.enable('trust proxy'); 

// HACK: Azure doesn't support X-Forwarded-Proto so we add it manually 
app.use(function(req, res, next) { 
    if(req.headers['x-arr-ssl'] && !req.headers['x-forwarded-proto']) { 
     req.headers['x-forwarded-proto'] = 'https'; 
    } 

    return next(); 
});