Web Development

Error 504 gateway timeout: how to fix it?

By David, on November 28, 2022, updated on March 6, 2023 - 10 min read

504 gateway timeout is an error whose resolution is often beyond the reach of the website visitor. It is indeed a server error. Whatever the case, visitor or administrator, we must know how to react to such a trouble.

There are several reasons why the server may be experiencing a timeout. It can be:

  • a downed server ;
  • an overloaded proxy server;
  • Incorrect proxy settings.

In this guide we explain what the 504 gateway timeout error means. We detail the possible causes and the solutions to remedy them.

504 Gateway timeout: What is it?

The 504 Gateway timeout error is part of the HTTP 5XX status code family. This class includes codes that notify that there is an error with the server. The latter cannot give an answer to the client’s request. This is why this family is called server error codes.

The 504 Gateway timeout notification appears when the execution of the request involves two web servers.

The first, which is often the main server, is waiting for a response from an upstream server.

This error is similar to the 502 Bad Gateway status. It reveals that the main server has received an invalid response from the second one.

Photo by geralt on Pixabay

Some variants of the 504 Gateway error message

In our browsers, the 504 error is displayed in different forms and messages. Here are some of the most common expressions used to describe it.

  • 504 Gateway Timeout;
  • NGINX 504 Gateway Timeout;
  • Gateway Timeout Error;
  • HTTP Error 504;
  • HTTP 504;
  • HTTP Error 504 – Gateway Timeout;
  • Error 504;
  • 504 Error;
  • Gateway Timeout (504);
  • A blank screen;
  • The page request was canceled because it took too long to complete;
  • This page does not work – The domain took too long to respond.

Some websites customize their 504 Gateway timeout error message. This can be interesting since if the message is worded well, it can ease the disappointment of their users.

What impact on SEO?

Like most 5xx status, the 504 error prevents a web page from loading. This affects the browsing experience. If this error occurs and lasts longer than it should, Google might de-index the affected page.

When Google’s bots come across such a failure in their web crawling, they initially consider it as temporary. They display the affected page from their cache while waiting for a solution to be found.

However, if the trouble lasts for more than 6 hours, Google will consider it a serious website-wide problem. This is a judgment that can affect the ranking of the site in the search results.

Photo by DiggityMarketing on Pixabay

What causes the 504 Gateway timeout error?

There are several situations that can cause the 504 Gateway timeout error notification to appear.

As said above, the first cause of this error message is the timeout.

This happens when the upstream server, for some reason, fails to serve a response to the main one. We then assume that the upstream server is either faulty or saturated.

The error message can also appear when the router of the local network used by the user is overloaded. The same situation occurs when the proxy server used is also overloaded.

But in this case, the failure is not limited to a single website. The error message appears no matter which page we try to load. Incorrect proxy settings on both sides can also create an HTTP 504 error.

What is the best solution to fix the 504 Gateway timeout error as a user?

To provide a real solution to an HTTP 504 error, it is necessary to master the details of the affected WordPress site. Since there are many variables involved, we’ll start with the simplest solutions.

Refresh the page

The easiest solution to try is to refresh the page. So let’s wait a few minutes and reload the page.

We can use the F5 function key on the computer keyboard to do this. Ideally, we should even empty the browser’s cache before refreshing the page.

To do both tasks at the same time, we can use the keyboard combination CTRL + F5.

At the same time, we can check if the trouble does not come from the browser we are using. To do this, let’s try to visit the website from another internet browser.

If after all these steps, the problem remains, we’ll check if we are the only ones concerned by this failure. There are indeed online tools that help to know if a site failure is general or only concerns us.

Restart network devices

The malfunction or saturation of devices such as the router or the modem of a local network can create a 504 error. By restarting it, the connection failure can be solved.

The order in which these devices are restarted is important for solving the trouble.

So we need to start from the outside in. We need to turn the equipment back on by leaving the ISP equipment to our main client device.

Check the proxy server settings

Between a network terminal and the Internet there may be a proxy server. It is used in particular to protect the user’s privacy. For example, it hides the user’s location from websites.

It should be said that it is not very obvious that proxy servers cause a 504 error but it can happen sometimes.

To check if our proxy servers are to blame, we just need to disable them. Then we need to refresh the page to see if the problem is resolved.

If you are not using a proxy, this is not relevant for you. However, it should be noted that it is possible to activate them without realizing it. To be sure, let’s check the proxy settings of our browser and computer.

Solving DNS issues

DNS issues on the client and/or server side can cause a 504 error notification to appear.

A DNS problem on the server side can be caused by the DNS server not responding.

It can also come from the Fully Qualified Domain Name (FQDN) not resolving to the correct IP address.

Many times, we see this situation after a WordPress site migration to a new host. This trouble can be avoided if we wait until the propagation of the domain’s DNS records is complete.

On the client side, the DNS problem is solved by clearing the local DNS cache.

  • To do this on Windows, we just need to type the command ipconfig/flushdns in the command prompt.
  • Under Mac OS, to clear the operating system cache, we need to open the terminal. Then we have to type the command sudo killall -HUP mDNSResponder. This command only works with the latest versions of Mac OS.

The DNS issue can also be solved by changing our DNS servers temporarily. Usually, Internet service providers assign us default DNS. We can change these to public DNS IPs temporarily.

Contact the website administrator and the internet service provider (ISP)

If after all the above steps, you can’t find a solution, you may need to contact the site administrator. He is able to inform us about the real issue of his platform. He’ll also be able to tell us when the site will be accessible again.

However, if the connection error concerns only us or a part of the visitors, we’ll have to look for our solution elsewhere. Perhaps a server of our ISP is down. To be sure, we have to contact it.

Consult the site later

This solution is the one we apply out of spite. If all our attempts to solve the problem fail, we have no choice but to wait.

We have to wait for our ISP or the website administrator to correct the error. Then we can visit the page later to check if the problem is solved.

How to fix 504 Gateway timeout error as an administrator?

As a website administrator, we’ll have to take several steps to find a solution.

Increase server resources

It must be said that lack of infrastructure is the most common cause of 504 error. If our server does not have enough resources to support the load of our website, we’ll encounter connection errors.

The only solution to solve the issue is to choose a server with a better infrastructure.

For example, if we have a WordPress site that generates a large volume of traffic, we’ll need more PHP Workers. These workers are provided by the hosting company and are used to execute requests on a WordPress site.

When all the PHP workers available to the server are busy, a queue is formed. When this queue becomes too long, the server does not process old queries anymore.

This can cause a 504 error. To avoid this, we must ask our hosting provider to increase the number of PHP workers. Our website will then be able to execute several orders at the same time.

Photo by ColossusCloud on Pixabay

Reduce your HTTP delays

When a connection between the client and the web server remains open for too long, it causes HTTP delays.

With WordPress sites for example, this happens when WordPress imports are running. The solution to this trouble lies in a faster internet connection.

Protect yourself from DDoS attacks, bots and spam

Malicious attacks have the ability to bring down our web server. They can do this by sending a large number of demands that are able to monopolize a large amount of resources and also pose a threat to our server.

If we suffer a DDoS attack or an attack from bots, our server may be overwhelmed. The same is true if our site is spammed. All this can cause 504 errors. As a result, our real users will no longer be able to access our website.

To prevent this, we need to protect our server from these possible threats. To do this, we need to perform a traffic scan in order to detect irregular patterns in the traffic.

We can start the scan from the IPs of the main clients. Thanks to this, we’ll know which of the clients generates the highest number of requests. If suddenly the bandwidth used by our server becomes enormous, this report will be of great help.

Let’s continue the examination by looking at the cache analysis report. There we’ll see the number of requests missing or bypassing the cache.

For extra security, we can use a WordPress security plugin.

It detects IP addresses and traffic of concern. It then blocks them. If we don’t have a WordPress plugin, we can ask our hosting provider to block certain IP addresses.

IP blocking can become a never-ending operation. Indeed, many malicious attackers after their blocking come back to the charge by changing proxy addresses and IPs.

Resolve database issues

A corrupted database can cause a 504 Gateway timeout error. This situation is particularly noticeable on WordPress sites. Corrupted files or tables are usually what damage the entire database.

A corrupted WordPress database can also be the result of a website hack. To fix the issue, you need to repair the database. This involves restoring to a version prior to the hacking and several other steps.

Check the themes and plugins of the website

Normally, themes and plugins don’t cause a 504 error. However, they sometimes generate a high number of requests that are not cached.

This creates a long queue and leads to the mobilization of a significant part of the server’s workers. In this case, the server does not consider the old requests anymore. This eventually leads to 504 errors.

Disabling plugins is the best solution to solve this failure.

In WordPress, to disable extensions, we just need to go to the dashboard and then click on the PLUGINS menu.

We’ll then select the option DISABLE. This is done in the grouped actions sub-menu. Then check all the extensions. Finally, we click on APPLY. All our extensions will be deactivated.

The deactivation can also be done via FTP. It consists in renaming the main directory of the extensions.

After deactivating all the extensions, let’s check if the website loads properly. If it is displayed correctly, we’ll then proceed to activate the extensions one after the other. With each activated plugin, we need to check if the site is working.

This procedure will help to identify the extensions that cause the 504 error. Finally, we need to make sure that WordPress is up to date.

Our website’s themes and extensions should also be up to date. We also need to check the version of PHP that our server uses. It should be the recommended version.

Checking the error register

Reading error registers is very useful when solving 504 errors, especially if you have a WordPress site. With this reading, you can reduce the amount of problems that exist on your site in a short period of time.

If your hosting provider does not provide access to a logging tool, you can use the WordPress debugging mode. We’ll enable it by adding the following code to our wp-config.php file.

define (‘WP_DEBUG’, true);
define (‘WP_DEBUG_LOG’, true);
define ('WP_DEBUG_DISPLAY', false);

The WP_ DEBUG constant is used to enable and disable WordPress debugging. To increase its functionality, this constant has two other constants.

WP_DEBUG_LOG indicates that all errors should be listed in the debug.log file. This file is located in the directory/wp-content/.

It is possible that we do not find this folder. In this case, we just have to create the document ourselves.

WP_DEBUG_DISPLAY makes sure that debugging logs are displayed in the HTML page. We can hide all errors by setting this constant to false. We can still check them afterwards. Setting the WP_DEBUG_LOG constant to true will help us do this.

We also have the possibility of downloading the WordPress error register. In this case, it will be a matter of downloading the raw files of the latter through FTP. They are usually located in the root directory of our server. We should see them by opening the folder called “logs“.

It is also interesting to read the server’s error register. Depending on our hosting server, these files can be found in different folders.

  • For Apache, we’ll find the files in /var/log/apache2/error.log/.
  • For Nginx, let’s check /var/log/nginx/error.log/.

Configure web servers properly

The 504 Gateway timeout error can be solved by modifying some web server configuration files. This will increase the resource limits for certain directives.

Changing resource limits on Apache

Let’s start by adding some code to our Apache server’s httpd.conf folder. This is the Timeout 600 code.

The integration of this code in the httpd.conf document allows us to increase the waiting time of the server for certain queries. It is only after this duration that the server will consider that the network timeout is exceeded. By default the timeout value is 60 seconds on Apache 2.4.

The addition of this directive must be done in the httpd.conf file and not in the .htaccess file. However, not all hosts allow the httpd.conf document to be modified.

In this case, to increase the resource limit, you must go to the .htaccess folder of your web server. Then change the value of the LimitRequestBody directive.

Then open the php.ini document. Add the line of code max_execution_time 300. By default, the value of this directive file is 30 seconds. Increasing it allows our website’s scripts to run for a longer time.

Changing the resource limits on Nginx

With Nginx, two uses are possible.

  • Either, you have configured your WordPress site with it.
  • Or, you use this server as a reverse proxy for Apache.

In both cases, a modification of the configuration allows to avoid 504 errors.

Settings on Nginx + FastCGI (PHP-FPM)

Nginx is often used with the FastCGI Process Manager. So we’ll start the modifications with the PHP-FPM folder. To find it you just have to go to:

/etc/php7.4/fpm/pool.d/www.conf

Once in the folder, we’ll set the request_terminate_timeout directive = 300. Let’s then go to the php.ini file of our server, it is located at:

/etc/php.ini

Let’s open the document, set the max_execution_time directive to 300 seconds.

So we should have max_execution_time = 300. Now let’s go to the nginx.conf file. In the location block of this file, we’ll add the code :

location ~. php $ {
...
fastcgi_read_timeout 300;
}

For the changes to take effect, we need to restart the server as well as PHP-FPM. To do so, let’s use these commands:

sudo service nginx reload
sudo service php7.4-fpm reload

Modification in the case where Nginx would be used as a reverse proxy to Apache.

Here we’ll modify the directives that concern the server timeouts. Let’s open the nginx.conf file. Then add the following directives:

proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;

At the end of the modifications, let’s reload the Nginx server with the code: sudo service nginx reload.

Other solutions to correct the 504 Gateway timeout error

Our server’s firewall may be at the root of the error. It may have incorrect settings. This prevents the server from establishing a good connection.

Let’s go to the error logs of the web server. Those who use a load balancer should also make sure that it does not cause network connectivity problems.

Timeout can occur if there is poor connectivity between web server and proxy server. The CDN (content delivery network) of our site can also create a 504 Gateway timeout.

So let’s disable the CDN (Cloudflare, Sucuri…) and reload the website to see if the issue is solved.

If no attempt to resolve the trouble works, it may be time to refer us to the hosting provider. This may be a sign that the issue is beyond our control. None of our applications are responsible.

The trouble may come from one of our host’s resources. We’ll contact them for assistance.

David