There are few things as frustrating and worrisome as getting 504 errors on your 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.
What is a 504 Gateway Timeout Error?
To understand the cause of a 504 Gateway Timeout error on your 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 your 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 your site is receiving too much uncached traffic, caching more of this traffic is a great first step.
Identify and Block Unwanted Traffic
In some cases, specific IP addresses or user-agents might be over-queueing your site with uncached requests. You can take a look at your Access Logs in the User Portal to identify any patterns.
- Login to the User Portal
- Select the unique environment name you wish to see logs for
- Click Access Logs
- Select the Apache tab to see only uncached requests
- Remember, only uncached requests can receive a 504
- Select the Nginx tab to see all requests, even static assets
If you identify any traffic from IP addresses or specific User Agents which you would like to block, use a security plugin like iThemes security or All-in-One WP Security and Firewall. You may also use a service such as Cloudflare to block bad actors.
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 you are seeing 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 your 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 your access logs and error logs and locate which URLs are receiving a 502 response most office. This typically will tell you the file/plugin/theme causing the issue. From there, disable the item, revert any recent changes or check for updates.
You may also need to look into optimizing your database to reduce TTFB. Having a high TTFB adds load to each page and could be a significant contributor to slow process completion on your website.
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.