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);
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.
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;
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With