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.
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.
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 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.
- Remember, only uncached requests can receive a 504.
- If there is any traffic from IP addresses or specific User Agents which should be blocked, this can be done using the Web Rules Engine in the User Portal.
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.
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.
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.
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