In my small experience, 502 Gateway error actually means that it's not about Nginx itself , it means the related application is somehow not responding.
I have a NodeJS and MongoDB running on my VPS , once my Node application was crashed because Mongo wasn't responding and I got this message from Nginx.