502 Bad Gateway error for my server running with Node JS on nginx proxy

I'm a little bit of a noob to this stuff, but I spent hours trying to debug my issue. It ended up being that my global variable for the port in my API wasn't being assigned correctly so node wasn't listening to the right port. Due to my infrequent use of a lot of this stuff, I wrote down how I should trouble-shoot it in the future and figured I would share. Again, this is for my API, so you may be slightly different (I was using port 3006).

  • In any console, double check you are getting an error by running wget https://example.com/api/v1/ping or if you are ssh'd into the production console you can do wget 127.0.0.1:3006/api/v1/ping
  • Make sure that node is running properly. After starting node in pm2 using npm run build then npm run start, make sure pm2 is running correctly with pm2 status
  • Check the error logs for pm2 with pm2 logs. This should show the normal output from console.log messages in your API. Does it say it is running on the correct port? You can have your api log your port at startup to confirm it is on the correct port. Check other errors as well. You can clear all logs in pm2 using pm2 flush
  • Now make sure that your server is listening to incoming traffic on the port that you specified sudo netstat -plunt will give you a list of ports that are open and the programs that are using them.
  • You can get more info on the node apps using sudo ps aux | grep node to get all node programs listening to ports and it will show the full path for the node file path. You can match the PID in the previous command to make sure your ports are matching.
  • Check the status of nginx to make sure it is running: sudo systemctl nginx status or replace status with start to get it started
  • Now make sure that your nginx ports are using that incoming port. Check the nginx error log with sudo vim /var/log/nginx/error.log to confirm your port and server name are correct. Every time you send your server a command it will record the 502 error here with an error message: 2021/02/20 03:05:28 [error] 26646#26646: *644 connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XX, server:example.com, request: "GET /api/v1/ping HTTP/1.1", upstream: "http://127.0.0.1:3006/api/v1/ping", host: "example.com"
  • You can check the status of your nginx config files to make sure you don’t have any errors in the config by doing sudo nginx -t
  • Check the Nginx config file in /etc/nginx/nginx.config. This file probably includes the sites-enabled/default config file. Mind as well check the sites-available/default config file as well to make sure all ports and server names are correct and making sure there are no duplicates (most likely would show up in the error logs).
  • For any changes that are made to the config files, you need to restart the nginx system using sudo service nginx restart

Hopefully that helps some of you! :) Happy Coding


502 errors are generally caused by NGINX being unable to pass a request to "upstream", in this case your Node.js server (which is also what the error message suggests: "Connection refused"").

It may be crashing and restarting, so check its logfiles to see what's causing the crashes.