Getting a 502 Bad gateway error?help?

After changing nameservers to digitalOcean and adding an a record to point to my ip address, i got the 502 badgateway error.ALso getting an error that says failed to connect to upstream.Everything was working untili added a dormain name.

Show comments

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Accepted Answer

Hi everyone i was able to solve my error.Permission issues caused the error.I basically gave the www-data ownership of /var/www. Here is how i did it:

sudo chown -R www-data:www-data /var/www

Then restart Ngnx ****sudo nginx -t && sudo systemctl restart nginx.

I hope this will help anyone having the same issue.

Bobby Iliev
Site Moderator
Site Moderator badge
April 7, 2020

Hi there @petergitz,

What I could suggest in this case is following the steps from this answer here on how to troubleshoot common Nginx problems:

Feel free to share the output of the commands here so that I could try to advise you further.

On a side note, keep in mind that changing your DNS records can not cause the 502 error, so it is most likely a problem with your Nginx webserver or your backend service is not running and you need to start it.

Hope that this helps! Regards, Bobby

TL;DR - If you’re using pm2 and you used the --watch flag, try stopping your process and restarting it without the --watch flag. See if that fixes it.

So, I just fixed the 502 error that I was dealing with for the past couple of hours and I figured that I’d share what the problem was here in case anyone wants to give it a shot as well and nothing else is fixing the problem for you.

If you are NOT using pm2, then this won’t help you.

I am using pm2 to keep my app alive. The other day I figured I’d include the --watch flag because it watches for changes in files. Being that I have been updating my site a lot recently I figured that this would be a good idea. So, today I was fooling around with creating subdomains and learning a little about how to put a blog on my site but on the subdomain blog.<domain>.com. That was all fine for the most part and eventually, I ended up deleting the test files I had created and symlinks, etc.

So, before I was going to close it all up for the day, I figured I’d do a quick run through to make sure everything was working correctly. Almost everything was… however, when I tried to navigate to a page that I expected to display a 404 Not Found error page, the upstream server closed the connection prematurely and I got a 502 Bad Gateway error. After hours of looking through my server configuration and running through threads about this problem (including this one) I decided that I’d try debugging it by just running npm start without pm2 keeping it alive so I can see the live server errors. That’s when I because very surprised. I tried navigating to a fake path on my url and the 404 error page successfully displayed. So, I then stopped the app from running and used pm2 again… this time without the --watch flag and tried again. It worked! That’s when I realized that I hadn’t used the --watch flag. I couldn’t believe that that was what caused the problem. I tried it again with the --watch flag and it broke. I then tried it again WITHOUT the --watch flag and it worked.

So long story short, I am not using pm2 <name> --watch to keep my app running. I am not sure why this happens but avoiding the --watch flag fixed the issue for me. It may or may not fig it for you but it’s worth a try.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel