cphill cphill - 3 months ago 110
Node.js Question

NodeJS connect() failed (111: Connection refused) while connecting to upstream

I am running into an issue today where all of a sudden my Elastic Beanstalk app is sending me to a

502 Bad Gateway
page. Now I have run into this issue in the past and the reason why this was happening was because the Node command could not start my server. I fixed this by inputting
Node command: node main.js
and I never ran into this issue until randomly this morning. All of a sudden it stopped working and I get this error, in my error log:

2015/03/31 13:07:17 [error] 697#0: *519 connect() failed (111: Connection refused) while connecting to upstream, client: 54.146.12.189, server: , request: "HEAD / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "54.152.12.19"
2015/03/31 13:07:17 [error] 697#0: *521 connect() failed (111: Connection refused) while connecting to upstream, client: 54.146.18.189, server: , request: "GET /clientaccesspolicy.xml HTTP/1.1", upstream: "http://127.0.0.1:8081/clientaccesspolicy.xml", host: "54.152.12.19"
2015/03/31 13:16:02 [error] 697#0: *523 connect() failed (111: Connection refused) while connecting to upstream, client: 69.204.65.1321, server: , request: "GET /blog/the-differences-in-segmenting-your-data-by-users-and-sessions HTTP/1.1", upstream: "http://127.0.0.1:8081/blog/the-differences-in-segmenting-your-data-by-users-and-sessions", host: "www.mywebsite.com"


How should I approach solving this issue?

Here is my
main.js
file:

//Load express
var express = require('express');
var app = express();
var router = express.Router(); // get an instance of the router
var bodyParser = require('body-parser'); // configure app to use bodyParser()
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var aws = require('aws-sdk');

app.use(bodyParser.urlencoded({ extended: true})); // get data from a POST method
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cookieParser());


var port = process.env.PORT || 8080; // set the port

var DB_CONFIG = process.env.DB_CONFIGURATION;
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = process.env.S3_BUCKET;

var blogDB = require('./config/blogDB.js');
mongoose.connect(blogDB.url);




require('./config/passport.js')(passport);


app.set('view engine', 'ejs'); // set ejs as the view engine

app.use(express.static(__dirname + '/public')); // set the public directory

app.use(session({ secret: 'thisisatest' }));
app.use(passport.initialize());
app.use(passport.session());

app.use(flash());


var routes = require('./app/routes');

app.use(routes); // use routes.js


app.listen(port);
console.log('magic is happening on port' + port);

Answer

A 502 Bad Gateway error usually suggests that the proxy (Nginx in NodeJS's case) can't find a destination to route the traffic to.

Looking at your original error logs, it looks like nginx is trying to go to http://127.0.0.1:8081. But your main.js has port 8080 as the fallback unless the ENV variable PORT is set.

I don't know if you are setting that variable, but try switching your NodeJS app to listen on port 8081 and see if that helps.

Additionally, I've written this answer that explains the NodeJS setup for traffic which might help: elastic beanstalk weird nginx configuration

If you still have issues, you might have to give some more info on your setup.