Getting a 502 Bad gateway error?help?

Posted April 7, 2020 4.7k views
DigitalOcean Accounts

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.

1 comment

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
3 answers

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.

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!

  • i run ngnx and syntax was ok and it was active.I’m having trouble giving www-data permission to my folders.Here are my configurations:

    Description=gunicorn socket
    Description=gunicorn daemon
    ExecStart=/home/peter/myenv/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/run/gunicorn.sock \
    sudo systemctl status gunicorn
    ● gunicorn.service - gunicorn daemon
       Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
       Active: failed (Result: exit-code) since Tue 2020-04-07 16:13:21 UTC; 35min ago
      Process: 19127 ExecStart=/home/peter/myenv/bin/gunicorn --access-logfile - --workers 3 --bind u
     Main PID: 19127 (code=exited, status=1/FAILURE)
    Apr 07 16:13:16 portfolio systemd[1]: Started gunicorn daemon.
    Apr 07 16:13:16 portfolio gunicorn[19127]: [2020-04-07 16:13:16 +0000] [19127] [INFO] Starting gu
    Apr 07 16:13:16 portfolio gunicorn[19127]: [2020-04-07 16:13:16 +0000] [19127] [ERROR] Retrying i
    Apr 07 16:13:17 portfolio gunicorn[19127]: [2020-04-07 16:13:17 +0000] [19127] [ERROR] Retrying i
    Apr 07 16:13:18 portfolio gunicorn[19127]: [2020-04-07 16:13:18 +0000] [19127] [ERROR] Retrying i
    Apr 07 16:13:19 portfolio gunicorn[19127]: [2020-04-07 16:13:19 +0000] [19127] [ERROR] Retrying i
    Apr 07 16:13:20 portfolio gunicorn[19127]: [2020-04-07 16:13:20 +0000] [19127] [ERROR] Retrying i
    Apr 07 16:13:21 portfolio gunicorn[19127]: [2020-04-07 16:13:21 +0000] [19127] [ERROR] Can't conn
    Apr 07 16:13:21 portfolio systemd[1]: gunicorn.service: Main process exited, code=exited, status=
    Apr 07 16:13:21 portfolio systemd[1]: gunicorn.service: Failed with result 'exit-code'.
    sudo tail -F /var/log/nginx/error.log
     r equest: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: ""
    2020/04/07 16:53:34 [error] 24897#24897: *335 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: ""
    edited by MattIPv4
  • I am unable to change ownership to www-data i think this is what is causing my problem because everything else is ok with ngnx

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.

edited by MattIPv4