Website errors · Article 0140
How to Fix a 504 Gateway Timeout Error
A 504 Gateway Timeout error occurs when a gateway or proxy does not receive a timely response from an upstream server. The delay may come from a slow application, overloaded database, unavailable backend, network problem, or an unsuitable timeout configuration. This guide shows how to identify the component that is taking too long and correct the underlying problem.
What does a 504 Gateway Timeout error mean?
HTTP 504 is a gateway timeout response. It means that a server acting as a gateway or proxy did not receive a response from another server within the time allowed for that request.
For example, a visitor requests a dynamic page. A reverse proxy forwards the request to an application server, but the application takes too long to respond. If the proxy reaches its waiting limit first, it may return a 504 response to the visitor.
The gateway may be a CDN, reverse proxy, web server, or load balancer. The upstream server may be an application server, database-dependent service, API, or another backend component.
A 504 does not necessarily mean that the website's main server is offline. The server may still respond to simple requests while a particular dynamic operation takes too long.
Common causes of a 504 error
- Slow application processing: A script or application request takes longer than the gateway's allowed response time.
- Database delays: Slow queries, database locks, connection exhaustion, or an overloaded database delay the application.
- Server resource exhaustion: CPU, memory, worker processes, or other resources become insufficient to handle requests.
- Unreachable upstream server: The gateway cannot establish a connection to the configured backend or receive its response.
- Network problems: Routing, connectivity, firewall rules, or intermittent network failures delay communication between services.
- External API delays: The application waits for a third-party API or service that responds too slowly.
- Proxy timeout mismatch: The gateway's waiting limit is shorter than the time a legitimate application operation needs.
- Traffic spikes: A sudden increase in requests causes queues and processing delays.
- CDN or hosting problems: An intermediary or hosting provider experiences an incident that affects communication with the origin server.
Step 1: Identify which requests are timing out
Start by determining whether the 504 affects the entire website or only specific operations. This distinction helps narrow down the responsible component.
- Open the homepage and record whether it loads successfully.
- Test the exact URL that originally returned 504.
- Try a static resource, such as an image or stylesheet, if one is available.
- Test another dynamic page or application function.
- Record the response status, timestamp, time zone, and any request ID or trace ID.
If static pages work but a dynamic page fails, investigate the application, database, and backend services. If nearly every request fails, investigate shared infrastructure, network connectivity, and the gateway itself.
Do not repeatedly refresh a slow endpoint. Repeated requests can increase the workload and make an overloaded application slower.
Step 2: Determine which server generated the 504 response
A website request may pass through several components before reaching the application. You need to identify which component returned the timeout.
- Open the browser's developer tools and select the Network panel.
- Repeat the failing request once.
- Select the failed request and inspect its status, response headers, and timing information.
- Record any server identifier, CDN diagnostic header, request ID, or trace ID.
- Compare the timestamp with the origin server and application logs.
Response headers can provide clues about whether the response came from a CDN, reverse proxy, or origin server. However, headers alone do not always prove which component generated the error.
If you manage the infrastructure, trace the same request through the gateway and upstream logs. If you use managed hosting, ask the provider to identify the component that returned the 504.
Step 3: Check application and server logs
Logs are often the most useful evidence when a gateway timeout occurs. Look for slow requests, application errors, worker exhaustion, and upstream connection problems around the exact failure time.
Depending on your hosting environment, relevant information may be available in:
- Web server access and error logs.
- Reverse proxy or load balancer logs.
- PHP or application runtime logs.
- Database slow-query and error logs.
- CDN or hosting provider diagnostic logs.
- Application performance monitoring reports.
Look for messages indicating that an upstream request timed out, a backend connection failed, a worker stopped responding, or an application operation exceeded its execution limit.
Compare multiple log entries using timestamps and request identifiers whenever possible. A message that appears near the failure time is useful evidence, but it should be connected to the affected request before changing configuration.
Step 4: Investigate slow application operations
If the gateway successfully connects to the application but the application responds too slowly, investigate the operation being performed.
Common sources of application delays include:
- Expensive database queries.
- Large reports or data exports.
- Unoptimized application code.
- Slow file processing or image operations.
- External API requests with long response times.
- Background jobs competing with visitor requests.
- Application workers waiting for unavailable dependencies.
Use application logs or performance monitoring to identify which operation consumes the most time. If a database query is responsible, review its execution plan and indexing requirements with an appropriate database administrator.
If an external API is slow, inspect its response time and error history. Consider whether the application needs a shorter dependency timeout, controlled retries, caching, or asynchronous processing.
Do not increase the gateway timeout before understanding why the application is slow. A longer waiting period can cause more requests to remain active and consume additional resources.
Step 5: Check database performance and connectivity
Many dynamic websites depend on database queries to generate pages. If the database is slow, overloaded, or unable to accept connections, the application may not respond before the gateway timeout expires.
Check the following:
- Database availability: Confirm that the database service is running and reachable from the application server.
- Connection limits: Check whether the database has reached its permitted connection capacity.
- Slow queries: Review slow-query logs or database performance reports.
- Locks and contention: Look for transactions or queries that hold resources for an unusually long time.
- Resource usage: Check CPU, memory, storage, and database workload during the failure window.
- Network connectivity: Confirm that the application can communicate with the database using the intended host and port.
If a specific query is responsible, optimize that query or the relevant data access pattern. Do not reset the database or delete records as a general response to a 504 error.
Step 6: Check upstream server connectivity
A gateway can return 504 if the upstream application server does not respond within the expected period. This can happen even when the gateway itself is functioning normally.
For an environment you administer, check:
- Whether the upstream application process is running.
- Whether the configured backend hostname and port are correct.
- Whether the gateway can establish a connection to the upstream server.
- Whether a firewall or network rule is interfering with the connection.
- Whether the upstream server is accepting requests or has exhausted its worker capacity.
- Whether the application responds slowly only under certain workloads.
For managed hosting, ask the provider to check the connection between the reverse proxy and the origin application. Do not change firewall rules or expose internal services without understanding the security implications.
Step 7: Review proxy and gateway timeout settings
Timeout settings control how long a gateway waits for an upstream operation. A mismatch between the gateway and application settings can cause a request to fail before the application finishes processing it.
For example, a reverse proxy might stop waiting after a configured interval even though the application is still working. The visitor then receives a 504 response.
When reviewing timeout configuration, identify the relevant setting for the actual software and request path. Depending on the architecture, this may involve:
- Upstream connection timeout.
- Upstream response or read timeout.
- Application execution time limit.
- Load balancer idle timeout.
- CDN origin response timeout.
- Database connection or query timeout.
Do not assume that every timeout setting controls the same stage of a request. A connection timeout, for example, is different from the time allowed for an established upstream connection to return data.
If the application performs a legitimate long-running operation, first consider whether it should be redesigned as a background task. If a longer timeout is genuinely necessary, make a measured adjustment that is supported by your hosting environment and consistent across relevant components.
For Nginx, Apache, PHP-FPM, and other server software, consult the official documentation for the exact directive and version before editing configuration files. A setting that is valid in one environment may not apply to another.
Step 8: Investigate CDN and reverse proxy issues
If your website uses a CDN or reverse proxy, the intermediary may be unable to obtain a timely response from the origin server.
- Check the CDN provider's service status and incident notices.
- Review origin connection and response-time diagnostics.
- Compare the failure timestamp with the origin server's access and error logs.
- Check whether the origin server is overloaded or responding slowly.
- Review recent changes to proxy, routing, or origin configuration.
- Contact the provider with the diagnostic request ID if the source remains unclear.
A direct-origin comparison may help an authorized administrator isolate the problem, but it should be performed using a secure and controlled method. Do not permanently disable the CDN or expose the origin server just to test the error.
Step 9: Check WordPress-specific causes
If the affected website runs WordPress, a 504 may be related to a slow plugin, theme operation, database query, scheduled task, or external service.
Use a cautious diagnostic process:
- Check the hosting control panel for resource-limit events and PHP errors.
- Review the WordPress and PHP logs around the failure time.
- Check whether the problem began after a plugin, theme, or WordPress update.
- Investigate whether a particular page, search, import, backup, or admin operation consistently triggers the timeout.
- If the evidence points to a plugin or theme, test the suspected component in a staging environment where possible.
- Apply a controlled correction or rollback only after confirming the responsible component.
Do not deactivate every plugin on a live production website as the first troubleshooting step. That may interrupt important features and does not identify the specific cause.
Step 10: Verify the correction
After correcting the underlying problem, repeat the original request and check whether the application now responds within the gateway's allowed time.
- Open the exact URL that previously returned 504.
- Confirm that the expected page or application result appears.
- Test another dynamic page and one static resource.
- Review application and proxy logs for new timeout events.
- Check database and server resource usage during normal traffic.
- Monitor the website to determine whether the error returns.
If you changed a timeout setting, confirm that the request completes successfully without causing excessive resource consumption or long-running connections.
Common mistakes to avoid
- Increasing every timeout: This may hide slow application processing and allow more requests to occupy workers for longer periods.
- Assuming the gateway is always responsible: The gateway may be working correctly while the upstream application or database is slow.
- Repeatedly refreshing the page: Additional requests can increase pressure on an already overloaded system.
- Restarting services without evidence: A restart may temporarily restore service but does not necessarily correct the underlying problem.
- Changing several configurations together: This makes it difficult to identify which change affected the result.
- Disabling security controls: Removing firewall or security protections can introduce new risks without resolving the timeout.
- Ignoring database performance: A slow query or exhausted connection pool can delay otherwise healthy application servers.
- Testing only the homepage: Other pages, APIs, forms, and administrative functions may still time out.
When to contact your hosting provider
Contact your hosting provider when you cannot inspect the gateway, upstream application, database, or infrastructure logs yourself.
Include the following details in your support request:
- The affected domain and exact URL.
- The date, time, and time zone of the failure.
- The HTTP status code and any available request or trace ID.
- Whether static pages work while dynamic requests fail.
- Whether the problem affects all visitors or only particular operations.
- Recent application, plugin, server, or configuration changes.
- Any relevant sanitized log messages.
Ask the provider to identify which gateway or upstream component timed out and to check application response time, database delays, worker capacity, and network connectivity.
Frequently asked questions
Is a 504 Gateway Timeout error caused by my internet connection?
Usually, a 504 is generated by a gateway or server while waiting for an upstream response. A visitor's local connection can affect access, but the status code itself generally points to a server-side communication or response-time problem.
Can a slow database cause a 504 error?
Yes. If an application waits for a database query or connection that takes too long, the gateway may reach its timeout before the application returns a response.
Will refreshing the page fix a 504 error?
It may work if the delay was temporary and the upstream service has recovered. Persistent timeouts require investigation of the application, database, gateway, or network rather than repeated refreshing.
Can a WordPress plugin cause a 504 error?
Yes. A plugin may perform expensive database operations, make slow external API requests, or trigger resource-intensive processing. Review logs and identify the specific operation before changing plugins.
Should I increase the server timeout to fix a 504?
Not as the first step. Identify why the upstream request is slow. Increase a timeout only when the operation legitimately needs more time and the relevant server resources and application design can support it.
What is the difference between a 502 and a 504 error?
A 502 Bad Gateway response generally indicates that a gateway or proxy received an invalid response from an upstream server. A 504 Gateway Timeout response indicates that the gateway did not receive a timely response from the upstream server.
Can a CDN cause a 504 error?
Yes. A CDN or reverse proxy may return a 504 when it cannot obtain a timely response from the origin server. Check the CDN diagnostics and origin logs to determine where the delay occurs.