Amazon Elastic负载均衡器不会填充x-forward-proto头

我试图强制所有http请求到https请求,我面临的问题是弹性负载平衡器不填充请求中的x-forward-proto头。

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

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); } }); 

这听起来像你的ELB监听器可能被configuration为TCP而不是HTTP。 configuration为TCP,它不会添加X-Forwarded-Proto或X-Forwarded-For。

发送http和https请求到两个不同的端口。 如果请求通过http端口发送,您可以安全地redirect它。