0
votes

I’m trying to deploy a next.js application using express to electric beanstalk and I’m getting constant 502 Bad Gateway errors.

2020/03/02 15:26:28 [error] 8286#0: *172 connect() failed (111: Connection refused) while connecting to upstream, client: 109.190.231.251, server: , request: "GET /db/websql/index.php?lang=en HTTP/1.1", upstream: "http://127.0.0.1:8081/db/websql/index.php?lang=en", host: "54.244.187.52"

I’ve read that this could be a port issue (:8081) so I have configured a PORT env variable in the software configuration. This isn’t fixing the issue. And my port is set as follows in my express server:



const PORT = process.env.PORT || 8081;

I’m using the

us-west-2

region.

I have an EB config file in the .ebextensions directory which reads 'npm start' and that looks like the following in my package.json



"start": "NODE_ENV=production next build && NODE_ENV=production node ./server.js",

Everything runs fine locally in both dev and prod modes but I can’t seem to get it running on EB.

Any help would be very much appreciated!

1
seems like it would be bad practice to run next build in the start command??? There must be a better way...NSjonas

1 Answers

0
votes

Go to the configuration of the environment Click on "Edit" from "Software" then set the enviroment variable "PORT" to the port you want to use for example node commonly use 3000

Also, make sure that your nextjs app is building right! Elastic beanstalk if you chose the Nodejs as platform when creating the env, is going to run npm install and then npm start. What you can do is at npm start script:

"start": "next build && next start -p THE_PORT_YOU_SET_AT_ENV",