Report this

What is the reason for this report?

How to solve ' 502 Bad Gateway ' nginx ??

Posted on March 29, 2021

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



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.