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.

Before you change anything: Record the affected URL, timestamp, and current configuration. Back up relevant files before editing server settings. Change one thing at a time, and never expose passwords, API keys, private customer data, or authentication tokens in diagnostic logs.

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

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.

  1. Open the homepage and record whether it loads successfully.
  2. Test the exact URL that originally returned 504.
  3. Try a static resource, such as an image or stylesheet, if one is available.
  4. Test another dynamic page or application function.
  5. 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.

  1. Open the browser's developer tools and select the Network panel.
  2. Repeat the failing request once.
  3. Select the failed request and inspect its status, response headers, and timing information.
  4. Record any server identifier, CDN diagnostic header, request ID, or trace ID.
  5. 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:

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:

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:

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:

  1. Whether the upstream application process is running.
  2. Whether the configured backend hostname and port are correct.
  3. Whether the gateway can establish a connection to the upstream server.
  4. Whether a firewall or network rule is interfering with the connection.
  5. Whether the upstream server is accepting requests or has exhausted its worker capacity.
  6. 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:

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.

  1. Check the CDN provider's service status and incident notices.
  2. Review origin connection and response-time diagnostics.
  3. Compare the failure timestamp with the origin server's access and error logs.
  4. Check whether the origin server is overloaded or responding slowly.
  5. Review recent changes to proxy, routing, or origin configuration.
  6. 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:

  1. Check the hosting control panel for resource-limit events and PHP errors.
  2. Review the WordPress and PHP logs around the failure time.
  3. Check whether the problem began after a plugin, theme, or WordPress update.
  4. Investigate whether a particular page, search, import, backup, or admin operation consistently triggers the timeout.
  5. If the evidence points to a plugin or theme, test the suspected component in a staging environment where possible.
  6. 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.

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

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:

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.

Related troubleshooting guides