5xx Server errors
Last updated Jul 26, 2023

504 - Gateway Timeout

Benjamin Bouchet
Get your free
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

The HTTP Status Code 504 means that server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

What is HTTP Status Code 504?

HTTP status code 504, also known as the Gateway Timeout error, is a part of the HTTP status codes, a set of standard response codes given by web servers on the internet. This status code indicates that a server acting as a gateway or proxy did not get a timely response from an upstream server it needed to access in order to complete the request.

Understanding the Gateway Timeout Error

To understand the HTTP status code 504, it's necessary to dive into the workings of the Hypertext Transfer Protocol (HTTP). When a client, such as your web browser, sends a request to a web server, the server sends back a response. This response includes an HTTP status code, which provides information about the status of the request.

In the case of HTTP status code 504, the status code indicates that a server, while acting as a gateway or proxy, was unable to receive a timely response from another server. This is typically an upstream server it needed to access to fulfill the request. It's important to note that the issue is not with your computer or internet connection, but somewhere in the network where the error occurred.

Factors Causing Gateway Timeout Errors

A multitude of factors can contribute to a gateway timeout error. For instance, server connectivity issues might be responsible for a server not being able to get a timely response from the upstream server. This could be due to problems with the server's network devices or a faulty firewall configuration.

Incorrect proxy settings are another common cause for this status code. If a proxy server's settings are incorrect, it may cause issues with the communication between servers.

Other times, DNS issues might be at play. The Domain Name System (DNS) translates domain names to IP addresses. However, if there's a problem with the DNS server or if the DNS cache needs to be flushed, it might result in a 504 error.

When and How is HTTP Status Code 504 used?

HTTP status code 504 comes into play when a server acting as a gateway or proxy is unable to get a timely response from an upstream server. It's crucial to note that the error is server-side, meaning it's usually out of the user's control.

Occurrences of the 504 Error

A 504 Gateway Timeout error can occur when you're visiting any website, whether it's a WordPress site or a social media platform. It can also happen regardless of the web server software in use, whether it's Nginx or something else.

If you are a user and you receive a 504 error, it's useful to try a few things like reloading the page, using a different browser, or checking your internet service provider for any known issues.

For server administrators, the error can often be resolved by checking the logs to identify any server connectivity issues or checking the proxy server settings. If the server uses Nginx, a command like 'service nginx reload' might be needed if changes to the configuration have been made.

Handling a 504 Error

For a software engineer or system administrator, handling a 504 Gateway Timeout error can involve several steps. Depending on where the error is coming from, they might need to adjust the load balancer, review the proxy server settings, or even check the PHP workers if it's a WordPress site.

If the problem is with an upstream server, you might need to confirm that it has the correct IP address or that it's not experiencing its own internal server error. If the error is happening frequently, it might be beneficial to look into adding more resources, such as additional servers or increased capacity for existing servers.

In some cases, the error might be out of your hands. If the problem lies with the origin server, you might need to get in touch with the service provider or the entity managing the origin server.

Resolving HTTP Status Code 504

Addressing a 504 Gateway Timeout error requires a tailored approach depending on the root cause of the problem. At times, editing the server config files to increase resource limits for specific directives can help resolve this status code.

For Apache Webservers, you can add specific code to your `httpd.conf` to define how long the server will wait for certain requests before marking it as a network timeout issue. Adjusting the value of the `LimitRequestBody` directive in your `.htaccess` file or increasing the `max_execution_time` directive in your `php.ini` file can be beneficial.

When dealing with Nginx Webservers, tweaking the server settings can help prevent 504 Gateway Timeout errors. For instance, on Nginx + FastCGI (PHP-FPM), you could edit your PHP-FPM pool config file and set a specific directive for `request_terminate_timeout`. Afterward, adjust the `max_execution_time` directive in your `php.ini` file and add code to your `nginx.conf` file's location block. Finally, reload Nginx and PHP-FPM for the changes to take effect.

Other HTTP errors like the 504 Gateway Timeout, such as the 500 Internal Server Error, occur on the server-side, signifying issues with the server rather than the client's request or network. Therefore, understanding and addressing server-side issues become paramount.

In some instances, the 504 error can be due to third-party plugins, themes, services, inefficient database queries, or a combination of these factors. As a software engineer, it's crucial to be aware of these potential pitfalls and devise strategies to prevent or quickly resolve them.

Example usage of HTTP Status Code 504

HTTP status code 504, also known as Gateway Timeout, is a server-side issue that usually arises when one server does not receive a timely response from another server that it was accessing while attempting to load the web page or fill another request by the browser.

Non-Technical Example

To make this concept more digestible for non-technical readers, let's consider a situation in a restaurant. You (the client) are seated at your table (the browser), and you place an order for a dish (the request for a webpage). Your order is taken by the waiter (the first server) who then goes to the kitchen (the upstream or second server) to get your dish prepared.

Under normal circumstances, the kitchen prepares your dish and the waiter brings it back to you in a timely manner. But what if the kitchen is overwhelmed with orders and can't prepare your dish promptly? The waiter, after waiting for some time (the "timeout" period), comes back to you and informs you that your order can't be delivered at the moment. This is akin to a 504 Gateway Timeout error – the first server (waiter) did not get a timely response from the second server (kitchen), causing the client (you) to receive a timeout error.

Technical Example

Now, for a more technical example. Let's consider a scenario where a web client (like a browser) makes a request to a server. This server acts as a gateway or proxy and is supposed to forward the request to another upstream server.

GET /resource HTTP/1.1
Host: www.example.com

The server, acting as a gateway, sends the request to the upstream server but doesn't receive a response within a certain timeframe.

GET /resource HTTP/1.1
Host: upstream.example.com

As a result, the gateway server sends back a 504 Gateway Timeout status code to the client.

HTTP/1.1 504 Gateway Timeout
Content-Type: text/html

504 Gateway Timeout

504 Gateway Timeout

nginx/1.14.0 (Ubuntu)

The above example shows how a 504 status code might be returned during an HTTP transaction. The actual timeout value can be configured within the server settings.

What is the history of HTTP Status Code 504?

HTTP status code 504, or "Gateway Timeout", is part of the HTTP/1.1 standard as defined by the Internet Engineering Task Force (IETF). The HTTP/1.1 standard, including status code 504, was officially released in 1997 as part of RFC 2068. The main goal of introducing this status code was to provide a specific error response for situations where a server acting as a gateway or proxy did not receive a timely response from an upstream server.

Over the years, the HTTP standard has evolved, but the status code 504 and its intended use have remained consistent. It's still widely used across the web, and understanding its implications is crucial for both website developers and administrators.

How does HTTP Status Code 504 relate to other status codes?

HTTP status code 504, along with other 500 error codes such as 502 and 503, fall under the category of server error responses. These codes are often indicative of an issue where the server is unable to fulfill a valid request. In general, they provide critical information for both the user and the website owner about why a page is not loading. Websites can employ logs and alerts to monitor how frequently these errors occur and be alerted when errors are happening, allowing for timely resolution.

Let's delve into the distinctions between HTTP status code 504 and other common server error responses like 502 and 503.

502 Bad Gateway

The 502 error signifies that a server acting as a reverse proxy for the website's origin server did not receive a valid response from the latter. This could be due to issues with the origin server, an incorrect or invalid DNS name, or a firewall on the origin server blocking the reverse proxy server's request. Additionally, this error might occur if requests at the origin server are taking too long to complete, and a caching tool like Varnish Cache has been instructed to timeout after a certain number of seconds. Varnish Cache, for instance, has a default timeout of 60 seconds.

503 Service Unavailable

The 503 error indicates that the website's origin server is temporarily unavailable. This could be a result of a crash on the server side or because the site is intentionally down for maintenance. This error is also common when a website receives more traffic than it can handle, an issue related to poor website scalability. For instance, Twitter's "Fail Whale," often displayed in the early days of the platform, is an example of a 503 error triggered due to high traffic volumes. To mitigate serving 503 errors because of an overflow of visitors, it's recommended to use a Content Delivery Network (CDN) and caching tools. This way, more requests can be served from the CDN and cache, reducing the load on the origin server.

504 Gateway Timeout

The 504 error is similar to the 502 error in that it occurs when the server acting as a proxy for the website's origin server does not receive a response from the origin server within a set time period. This could indicate an issue with the DNS host or hosting company, or a problem with the connection or configuration between the reverse proxy servers and the website origin server.

In essence, 502 and 504 errors are both related to a bad gateway, where the reverse proxy server is operational, but something it needs to collect from the origin server isn't working, or the connection between the reverse proxy server and the origin server is broken. Troubleshooting these errors involves ensuring the origin server and all the servers it needs to access are running correctly, checking the configuration between the origin server and reverse proxy server, and verifying that the firewall isn't unintentionally blocking legitimate traffic.

Conversely, 503 errors are often expected during website downtime for updates or changes and can also be triggered by a large influx of traffic. If a website doesn't scale well, it may be worthwhile to explore the use of a CDN or a caching reverse proxy like Varnish Cache.

Get your free
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required