18

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);
cphill
  • 5,596
  • 16
  • 89
  • 182

6 Answers6

21

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.

Community
  • 1
  • 1
Josh Davis
  • 6,651
  • 2
  • 25
  • 25
  • Thanks for the solution. In my case, nginx was trying for port 8080 which I have found from the logs. Then I have changed the port in the environment variable and it was working awesome. ---------------------------------------- /var/log/nginx/error.log ---------------------------------------- 2021/11/17 03:00:51 [error] 4105#4105: *10072 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.40.153, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.36.155" – Alwin Jose Nov 17 '21 at 04:09
3
upstream: "http://[::1]:5555/uploads/logo/df0944721b740b98c10a652ce0dd8296-640.jpg",

If you are having errors with upstream set to ipv6 --> [::1], replace localhost to 127.0.0.1 in your nginx conf.

server {
listen 80;

server_name mydomain.com;

location / {
    client_max_body_size 20M;
    client_body_buffer_size 128k;
    #proxy_pass http://localhost:5552;
    proxy_pass http://127.0.0.1:5552;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
}
keithics
  • 8,576
  • 2
  • 48
  • 35
2

It's important to add the Node command at Configuration Modify software section of your beanstalk app, if your app you are using the command start, so use it as node command, "npm start" that will start your app correctly, this also happens when use the folder and file bin/www to start the Nodejs server.

1

I know that this is a super old post, but I just had the same issue with my node server being restarted by my hosting provider. When the server reset it also caused mongoDB to be shut down.

When using forever to try to restart the node servers:

2018/04/12 06:49:32 [error] 23434#23434: *27 connect() failed (111: Connection refused) while connecting to upstream, client:

The log files do not indicate it's an error with mongo specifically but if you try to start the server manually:

node /server/server.js

Connection fails: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

Once mongoDB is restarted the server can be restarted:

sudo mongod &

And then simply restart your server and you're good to go.

Husk Rekoms
  • 483
  • 12
  • 22
0

Answer

These all point to no http server response, which means your http server is not answering requests.

The exact part that points this out in the logs is the following.

connect() failed (111: Connection refused) while connecting to upstream
server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:

Make sure you have taken steps to start that, and ensure its currently running at the time the error is generated.

It can also possibly point to configuration error on the the http server js file itself.

To get a better idea, view error logs in /var/log/nginx/error.log. If you see connection errors to your proxy backend, like above, then that is probably the case.

To see if its having a problem connecting to a proxy that was defined. Make sure the node process manager is running and configured correctly on that proxied port. e.g. If your running a node app, look for pm2, or whatever node module you use to start your http server with.

pm2 show

The OA assumed your backed was simply misconfigured, and wasnt clear that an entire http server was needed behind it. Some users are trying to launch apps that contain task managers that are trying to launch things that done exist, and dont realize entire back-end was not needed or running, like myself at the time being.

blamb
  • 4,220
  • 4
  • 32
  • 50
0

Under beanstalk, go to configuration, then to software, add npm start on node command. This will be your default settings. Nginx works fine with its default settings

Ally Makongo
  • 269
  • 5
  • 14