502 Bad Gateway on nginx/1.2.6 using Elastic Beanstalk and Node.js

The quick fix for this is for express/http server  to listen on process.env.PORT,  not on a hard coded port (e.g. 3000). This can be accomplished in express by using

var express = require(‘express’);
var app = express();
app.set(‘port’, process.env.PORT || 8081);
…..
app.listen(app.get(‘port’));

 

I could not find any options or commands to configure the port for node in Elastic Beanstalk. At the moment, Nginx on an aws 64 bit linux instance is connecting to port 8081 to the node instance. process.env.PORT is set to 8081. When Express was listening on 3000 the bad gateway error was showing up. The logs/events provided by EB were useful to get to the root of this.

  • James

    THANK YOU. This was driving me nuts. It’s confusing because their sample app has a hardcoded port 3000 (admittedly not using Express).

  • RTimal

    Glad you found it helpful!

  • Matthew

    Totally worked. Thanks man.

  • Title

    Thank you.

  • RTimal

    no problem!