I configured the ELB to open up both 80 and 443, where 443 is configured with SSL. Both ELB ports points to the instances' 80 port. The ELB's heatlh check is using ping target HTTP:80/index.html. It used to work, until I recently decided to start redirecting http to https.
Now the following codes are on the server.js(the codes within // are my newly added codes):
//
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'));
});
I supposed the above request will redirect all request to the elb server but with https protocol.
However the server start printing:
redirecting
https://10.x.x.xx/index.html
And then the ELB is failing as https://10.x.x.xx/inde.html is not available.
However the index.html's location right under the {domain}/.
I think the way I redirect may be wrong - but I have no idea how to fix it.