【发布时间】:2017-02-28 06:06:54
【问题描述】:
我将 ELB 配置为同时打开 80 和 443,其中 443 配置了 SSL。两个 ELB 端口都指向实例的 80 端口。 ELB 的 heatlh 检查使用 ping 目标 HTTP:80/index.html。它曾经可以工作,直到我最近决定开始将 http 重定向到 https。
现在server.js上有以下代码(//内的代码是我新添加的代码):
//
app.use(function(req, res, next) {
if (config.env === 'prod' && req.get('X-Forwarded-Proto') !== 'https') {
console.log("redirecting")
console.log('https://' + req.get('Host') + req.url)
res.set('X-Forwarded-Proto', 'https');
res.redirect('https://' + req.get('Host') + req.url);
}
else
next();
});
//
app.use(express.static(path.join(__dirname, 'home')));
app.set('trust proxy'); // enable this to trust the proxy
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
app.get("/*", function(req, res, next) {
res.sendFile(path.join(__dirname, 'home/index.html'));
});
我认为上述请求会将所有请求重定向到 elb 服务器,但使用 https 协议。
但是服务器开始打印:
redirecting
https://10.x.x.xx/index.html
然后 ELB 失败,因为 https://10.x.x.xx/inde.html 不可用。
但是 index.html 的位置就在 {domain}/ 之下。
我认为我重定向的方式可能是错误的 - 但我不知道如何解决它。
【问题讨论】:
标签: node.js amazon-web-services redirect amazon-elb elastic-load-balancer