title: NodeJS behind AWS ELB redirecting http to https failed health check
body: <p>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.</p>
  
  <p>Now the following codes are on the server.js(the codes within // are my newly added codes):</p>
  
  <pre><code>//
  app.use(function(req, res, next) {
      if (config.env === 'prod' &amp;&amp; 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'));
  });
  </code></pre>
  
  <p>I supposed the above request will redirect all request to the elb server but with https protocol.</p>
  
  <p>However the server start printing:</p>
  
  <pre><code>redirecting
  https://10.x.x.xx/index.html
  </code></pre>
  
  <p>And then the ELB is failing as <a href="https://10.x.x.xx/inde.html" rel="nofollow noreferrer">https://10.x.x.xx/inde.html</a> is not available.</p>
  
  <p>However the index.html's location right under the {domain}/.</p>
  
  <p>I think the way I redirect may be wrong - but I have no idea how to fix it.</p>