6

我正在尝试将所有 http 请求强制为 https 请求,并且由于弹性负载均衡器未在请求中填充 x-forwarded-proto 标头,因此我面临着问题。

这是我正在使用的代码,因此导致重定向循环。我将如何解决这个问题?

app.use (function (req, res, next) {
    console.log('Request headers = ' + JSON.stringify(req.headers));
    console.log('Request protocol = ' + JSON.stringify(req.protocol));
    var schema = (req.headers['x-forwarded-proto'] || '').toLowerCase();
    if (schema === 'https') {
       next();
    } else {
       res.redirect('https://' + req.headers.host + req.url);
    }
});
4

2 回答 2

13

听起来您的 ELB 侦听器可能配置为 TCP 而不是 HTTP。为 TCP 配置,它不会添加 X-Forwarded-Proto 或 X-Forwarded-For。

于 2013-09-28T02:18:55.053 回答
1

将 http 和 https 请求发送到两个不同的端口。如果请求来自 http 端口,您可以安全地重定向它。

于 2013-09-27T13:10:18.010 回答