504 Gateway Timeout Error

There are few things as frustrating and worrisome as getting 504 errors on a site during a peak traffic period. Below we’ve included information on why 504 errors occur, how to resolve a 504 error and how to prevent 504 errors.

WP Engine Support is also on stand-by 365, 24×7 to help should you start seeing any 504 errors, or if you need help identifying the cause of 504 errors.

About 504 Errors

To understand the cause of a 504 Gateway Timeout error on a site, it’s best to first understand what a gateway timing out means. On WP Engine environments, the “gateway” refers to the uncached request queueing system.

On a server there are a specific number of “PHP workers”. Each uncached request on a server has to be fulfilled by one of these PHP workers.

When all PHP workers are busy, a backlog queue of requests builds up. The backlog feeds the longest-waiting queued request to the next available PHP worker. Having this backlog means additional uncached page requests can come in and still be served, even when all workers are already busy.

Read more about PHP workers and how WP Engine servers prioritize backlogged requests.

When all the PHP workers are full and the backlog is full, this is where 504 errors can begin. When a new request comes in at this point, older requests are then “evicted” from the backlog queue to keep requests streamlined and allow for new traffic to continue being served.

When a request is evicted from the PHP worker backlog queue, a 504 error occurs.

The recommended solutions to resolve 504 errors include:

  1. Increase Caching
  2. Identify and Block Unwanted Traffic
  3. Investigate and Resolve Related 502 Errors
  4. Increase PHP Workers

If 504 errors persist, reach out to WP Engine’s 24-hour Support.

Increase Caching

Only uncached traffic can receive a 504 error, because cached requests do not need to queue for PHP workers. If the site is receiving too much uncached traffic, caching more of this traffic is a great first step.

By default, page cache is set to 600 seconds, or 10 minutes. Static assets (images, CSS, JavaScript, etc) are set to 365 days.

By extending the cache expiration time for pages or resources, the amount of pages being served from cache will also be increased. We suggest taking a look at the Tips for Improving Cacheability article for more guidance. We also suggest reviewing our guide covering how to brace for a surge of traffic.

Identify and Block Unwanted Traffic

In some cases, specific IP addresses or user-agents might be over-queueing the site with uncached requests. Take a look at the site’s Access Logs and Error Logs in the User Portal to identify any patterns.

504 and 502 Errors

502 Bad Gateway errors may look deceptively similar to 504 Gateway Timeout errors, but the reasons behind them are different. 502 errors happen when a request being processed by a PHP worker takes longer than 60 seconds to complete and is automatically rejected.

Learn more about 502 errors in our guide.

If both 502 errors and 504 errors happen simultaneously this means that too many long requests are happening at once. Long-running processes can stay lodged in the available PHP Workers queue, and prevent many backlogged requests from being handled. After 60 seconds any requests that are still processing are cut off and rejected from queue. Until that point, however, that PHP worker is occupied and is not able to take another request from the queue.

To resolve the 504 in this case, it’s best to address the long-running processes and the 502 error first.

To do so, first review the access logs and error logs and locate which URLs are receiving a 502 response most office. This typically help identify which file/plugin/theme is causing the issue. From there, disable the item, revert any recent changes or check for updates.

Check out our full guide on how to troubleshoot and resolve 502 errors.

Additionally, having a high TTFB adds load to each page and could be a significant contributor to slow process completion on the website. Some sites may need to have the database optimized to reduce TTFB.

Learn more about troubleshooting and resolving high TTFB.

Increase PHP Workers

If you know your site will be receiving a large influx of uncached traffic and you aren’t able to increase the number of pages served from cache, and the traffic coming into your site is legitimate, it may be time to contact your Account Manager to make sure your server is prepared to handle this extra traffic.

If you are unsure of who your Account Manager is, simply reach out to Support in User Portal via 24/7 chat and we will help you get in touch.

NEXT STEP: Learn how to troubleshoot a 502 error

Still need help? Contact support!

We offer support 24 hours a day, 7 days a week, 365 days a year. Log in to your account to get expert one-on-one help.

The best in WordPress hosting.

See why more customers prefer WP Engine over the competition.