What exactly is a 502 Bad Gateway Error?
The 502 Bad Gateway error signals a request for either a page, script, process, or query has taken too long to complete and has been rejected from the server. The digits, 5xx and 02, are HTTP Status Codes which correspond to 5xx meaning Server Error while 02 refers to Bad Gateway.
To understand these errors, it’s important to know: what gateway was deemed “bad” and why? On the WP Engine 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.
On WP Engine we have a 60-second timeout for long processes. So if a PHP worker has to process a request for 60 seconds or more, that request will be rejected and will produce the 502 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, WP Engine has a list of recommended consultants you can reference.
If you need immediate help with 502 errors please contact Support via 24/7 chat in your User Portal.
Prevention
The best way to prevent issues with 502 Bad Gateway errors is to ensure your site is healthy. Following WordPress best practices for site health will help prevent code conflicts and keep your site secure.
Simple steps like updating your Plugins, Themes, and WordPress itself can help ensure site health. These updates should be tested in a staging or test environment first to prevent code conflicts on your production site.
Last, you should regularly audit your plugins and settings to delete anything unnecessary. Fewer installed plugins means a smaller codebase to maintain and fewer opportunities for code conflicts.
Can the timeout be turned off?
The 60-second timeout is in place to ensure our servers are able to serve the maximum amount of concurrent requests. Without a timeout in place, we’ve seen requests take minutes to even hours to complete. With the continued performance of your site in mind, we will not be able to disable the timeout. This timeout is programmed at the root of our platform. It is in place to ensure optimal server health, speed, and uptime.