Amazon Elastic 负载均衡器未填充 x-forwarded-proto 标头 [英] Amazon Elastic load balancer is not populating x-forwarded-proto header

查看:32
本文介绍了Amazon Elastic 负载均衡器未填充 x-forwarded-proto 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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.

这篇关于Amazon Elastic 负载均衡器未填充 x-forwarded-proto 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆