How To Fix 504 Gateway Time-out on Nginx

Fix 504 Gateway Time-out on Nginx

In this tutorial, we will show you how to fix 504 gateway time-out on the Nginx. If you are running a website that is powered by Nginx, you might have come across the 504 Gateway Time-out error. This error message indicates that the server was unable to complete the request made by the client in the specified time frame. There are several possible causes of a 504 Gateway Time-out error in Nginx, including:

  • Slow response time from the upstream server: If the upstream server takes too long to respond to Nginx’s request, Nginx will eventually give up and return a 504 error.
  • Excessive traffic: If the upstream server is handling too much traffic, it may not be able to respond quickly enough to Nginx’s requests, leading to a 504 error.
  • Network connectivity issues: If there is a problem with the network connectivity between Nginx and the upstream server, the request may time out, resulting in a 504 error.
  • Configuration issues: If the Nginx configuration is not set up correctly, it may cause a 504 error. For example, if the timeout setting is too low, Nginx may give up before the upstream server has a chance to respond.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step fixing of 504 Gateway Time-out.


  • A server running one of the following operating systems: RHEL-based such as CentOS, AlmaLinux, or Rocky Linux.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Fix 504 Gateway Time-out on Nginx

  • Changes in php.ini

Try raising the max_execution_time setting in php.ini a file (The CentOS path is /etc/php.ini):

max_execution_time = 150
  • Changes in PHP-FPM

Try raising the request_terminate_timeout setting in the php.ini file (CentOS path is /etc/php-fpm.d):

request_terminate_timeout = 150
  • Changes in Nginx Config

Finally, add the fastcgi_read_timeout variable inside our Nginx virtual host configuration:

location ~* \.php$ {
    include         fastcgi_params;
    fastcgi_index   index.php;
    fastcgi_read_timeout 150;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;

Save and close the file, then restart the Nginx web server and PHP-FPM to apply the changes:

sudo systemctl restart php-fpm
sudo systemctl restart nginx

For Nginx as Proxy for the Apache web server, this is what you have to try to fix the 504 Gateway Timeout error:

Add the following variables to nginx.conf file:

proxy_connect_timeout       300;
proxy_send_timeout          300;
proxy_read_timeout          300;
send_timeout                300;

Once complete, simply reload Nginx:

sudo nginx -t
sudo systemctl restart nginx

Testing the Solution

  • A. Load Testing:

Evaluate the effectiveness of your optimizations by conducting load tests. Use tools like Apache JMeter or Siege to simulate heavy traffic on your website and monitor its performance.

  • B. Real-World Scenario Example:

Here is a case study of a website that faced frequent 504 errors due to backend delays. By implementing the strategies mentioned above, they observed a significant improvement in response times, eliminating the 504 error entirely.

Congratulations! You have successfully fixed the error Nginx 504 gateway time out. Thanks for using this tutorial to fix 504 gateway timeout errors in the Linux system. For additional help or useful information, we recommend you check the official Nginx website.

Nginx Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!




r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button