Amazon Elastic load balancer is not populating x-forwarded-proto header

无人久伴 提交于 2019-11-30 05:36:30

问题


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

回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!