Question

How to solve ' 502 Bad Gateway ' nginx ??

Posted March 29, 2021 786 views
NginxNode.jsDeployment

If I run on my droplet ubuntu 20.04 with nginx a simple test nodejs server, it works:

const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

but when I try to update it with my more complex node server I get back ’ 502 Bad Gateway ’.

error.log:

2021/03/29 10:49:21 [error] 1430#1430: *1352 connect() failed (111: Connection refused) while connecting to upstream, client: 2.45.152.97, server: ferramentadenicolo.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3000/favicon.ico", host: "www.ferramentadenicolo.com"

Trying to understand what give me back this error, I noticed that just adding to the simple test a string like:

const api = require('./routes/api');

it returns me the 502

Some advice please?
I’m really new about deploying a server and I don’t know how to proceed.
THX

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hey, @roccoden91 have you checked your port configuration in the “complex node server” setup and made sure that it is matching your Nginx port forwarding?

Post your configs here, so we can take a look and give you some feedback.