I am uploading a basic node application to aws elastic beanstalk and i am getting a 502 bad gateway response when I go to the environment

Viewed 268

I uploaded the following file named server.js:

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

app.get('/', function (req, res) {
    console.log('Send message on get request');
    res.send('Hello full-stack development!');
});

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

var server = app.listen(app.get('port'), function () {
    console.log('Express server listening on port:' + 
    server.address().port);
}); 

along with the following package.json file:

{
    "name": "newswatcher",
    "version": "0.0.0",
    "description": "NewsWatcher",
    "main": "server.js",
    "author": {
        "name": "myname",
        "email": "myemail"
    },
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "4.17.1"
    }
}

and the package-lock.json and the npm modules. I followed the basic steps to create an aws application and then upload and deploy the zip file with the four files I have mentioned in and have set the environment variable NPM_CONFIG_PRODUCTION to true. Then when I go to the environment I get the 502 bad gateway error.

It works fine when I run it locally with npm start.

This is from the logs which make it seems like it is working:


/var/log/web.stdout.log


Jan 10 01:46:27 ip-172-31-13-23 web: > newswatcher@0.0.0 start /var/app/current

Jan 10 01:46:27 ip-172-31-13-23 web: > node server.js

Jan 10 01:46:27 ip-172-31-13-23 web: Express server listening on port:3000

Jan 10 01:49:59 ip-172-31-13-23 web: > Elastic-Beanstalk-Sample-App@0.0.1 start /var/app/current

Jan 10 01:49:59 ip-172-31-13-23 web: > node app.js

Jan 10 01:49:59 ip-172-31-13-23 web: Server running at http://127.0.0.1:8080/

Jan 10 01:58:11 ip-172-31-13-23 web: > newswatcher@0.0.0 start /var/app/current

Jan 10 01:58:11 ip-172-31-13-23 web: > node server.js

Jan 10 01:58:11 ip-172-31-13-23 web: Express server listening on port:3000

This is from the error logs:

2021/01/10 02:24:23 [error] 4619#0: *431 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.24.137, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.13.23"

2021/01/10 02:24:23 [error] 4619#0: *433 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.10.27, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.13.23"

These go on and on like this.

What is wrong?

1 Answers

Based on the comments.

The issue was caused by the fact that the express was litenting on port 3000, but nginx expected port 8080.The solution was to use port 3000 for nginx.

Related