我正在使用以下命令將所有http請求重定向到https請求。在node.js中使用EBS和ELB環境將http轉發到https
我可以從日誌中看到標題'x-forwarded-proto'永遠不會被填充並且是未定義的。
app.get('*', function(req, res, next) {
//http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#x-forwarded-proto
if (req.headers['x-forwarded-proto'] != "https") {
res.redirect('https://' + req.get('host') + req.url);
} else {
next();
}
});
它導致重定向循環。如何在沒有循環的情況下正確重定向?
嘗試了您的建議,仍然無法正常工作。 Elastic Load Balancer沒有設置正確的標題。 'res.set('x-forwarded-proto','https');'不起作用。 – user883499
找出爲什麼亞馬遜沒有設置標題,或嘗試我的第二種技術。我注意到'res.header'與'res.set'完全相同,對不起! – Plato
第二種技術也不適用,因爲負載平衡器總是嚮應用程序提供http(req.secure always false)請求。 – user883499