我正在使用以下方法将所有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();
}
});
这会导致重定向循环。如何在没有循环的情况下正确地进行重定向?
req.protocol
来检测请求是通过http
还是https
发送的。 - Brandon Clarkrouter.use(function(req, res, next) { if (req.header('X-Forwarded-Proto') == 'https') next(); else res.redirect('https://' + req.host + req.url); });
关于 X-Forwarded-Proto 标头的更多详细信息可以在此处找到:https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/ - DauleDK