To understand these errors, it’s important to know: what gateway was deemed “bad” and why? On the platform, the “gateway” these errors reference is the uncached request queue. Servers have a specific number of PHP Workers available to process uncached requests at a time. If a request being processed by a PHP Worker is rejected for taking too long to complete, this causes the 502 Bad Gateway error.
RESOLVING 502 BAD GATEWAY ERRORS
Now that we know the cause of 502 Bad Gateway errors, we can identify potential solutions. Some common reasons a request may time out can include: large data imports or exports, lengthy scheduled cron tasks, killed queries, and unoptimized site code.
For cron tasks, imports, and exports that will take over 60 seconds, it’s best to break it into smaller batches. This way all the smaller batches can complete in under 60 seconds each. Doing this also has the added bonus of reducing strain on your server environment.
You can use your site’s error logs to see if there have been any code errors which might explain the timeout, as well as killed queries. If these logs don’t help, you can start narrowing down root causes.
For example, turning off all plugins and seeing if the error still exists will help you determine if the error is caused by a plugin or by your theme.
Combating 502 errors can be an art in itself. Since the causes of these errors can be deep in the code of a site and not easily identifiable, we highly recommend consulting with a developer. If you need help finding a developer will assist you.