'connect() failed (111: Connection refused) while connecting to upstream.(node / nestjs) amazon Elastic

Trying to deploy my first app (Back-end). But I meet an error of the type 502 Bad Gateway.

2022/04/16 09:40:14 [error] 4853#4853: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.46.123, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.12.175"
2022/04/16 09:40:16 [error] 4853#4853: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.6.213, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.12.175"

This is my main.ts file:-

async function bootstrap() {
  const PORT = process.env.PORT || 8081;
  const app = await NestFactory.create(AppModule, { cors: true });
  app.useGlobalPipes(new ValidationPipe());
  const configService = app.get(ConfigService);
  app.enableCors();
  await app.listen(process.env.PORT || 8081, () => {
    console.log(`⛱ Talent Zea Cloud listening on port ${PORT}`);
    console.log('Press Ctrl+C to quit.');
  });
}
bootstrap();


Solution 1:[1]

EB uses port 8080 by default for node.js, but you are using port 8081. So either change your port, or setup PORT env variable in EB to 8081. From docs:

By default, Elastic Beanstalk configures the proxy to forward requests to your application on port 8080.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin