Amazon Elastic负载均衡器未填充x-forwarded-proto标头

use*_*499 6 load-balancing amazon-ec2 amazon-web-services node.js express

我试图强制所有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);
    }
});
Run Code Online (Sandbox Code Playgroud)

Mic*_*bot 13

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