I started to work with Elastic Beanstalk on AWS recently and ran into an issue when trying to deploy a Node.js app that I thought might be worth sharing. I created a test Node.js and Express application locally that worked fine and I then tried deploying using the Elastic Beanstalk console. Everything appeared to deploy correctly but when I tried accessing my url I received the following error “502 Bad Gateway” on “nginx/1.2.6”.

bad gateway

Unfortunately this error message didn’t give me much to go on, but when I looked in the Elastic Beanstalk log files I saw the following entry:

2013/05/18 13:33:37 [error] 1708#0: *11 connect() failed (111: Connection refused) while connecting to upstream, client:, server: , request: "GET / HTTP/1.1", upstream: "", host: "fitfavorites.elasticbeanstalk.com"

The confusing part of this error message for me was the port number of 8081, that was not the port I was expecting Beanstalk to utilize for my site. Looking at the code that was working locally I realized I had hard coded the port number to 3000. I quickly modified my configuration code with the following snippet. This code allows the application to use either a port setting set in the environment variables or default to 3000 if no port is configured.

var express = require('express');
var app = express();

app.set('port', process.env.PORT || 3000); ....

After re-deploying this to AWS, sure enough my problem disappeared. The one thing that was of interest to me was that I did not have to set an environment variable, it appears that Beanstalk has this set already for me.

I hope this helps anyone else who runs into this same issue. Have fun with Node on Elastic Beanstalk!


Feel free to comment on the post but keep it clean and on topic.

comments powered by Disqus