I am trying to force all http requests to https requests and I am facing the problem as elastic load balancer not populating x-forwarded-proto header in the request.
This is the code I am using and it is causing redirect loop because of this. How would I fix this problem?
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);
}
});
It sounds like your ELB listeners might be configured for TCP instead of HTTP. Configured for TCP, it will not add X-Forwarded-Proto or X-Forwarded-For.
Send http and https requests to two different ports. If the request comes through on the http port, you would be safe to redirect it.
来源:https://stackoverflow.com/questions/19049320/amazon-elastic-load-balancer-is-not-populating-x-forwarded-proto-header