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.