Methods of payment Abuse

Fix 504 gateway time out Nginx

20.02.2024, 23:31

A 504 Gateway Timeout error in Nginx means that the server running Nginx was unable to receive a response from the proxy server within the specified time period. This usually happens because the proxy server cannot process the request within the specified time or because of connection problems between Nginx and the proxy server.

Why 504 Gateway Timeout appears

The error occurs when the Nginx server is running in proxy mode. This happens when php-fpm or Apache is used. If we translate it from English to Russian, we get the following - the time to wait for a response from the server has been exceeded.

Several reasons for this behavior:

  • PHP or other language script has completely hung and will not return any response anymore;
  • the script is running for a very long time, but Nginx has an interval configured to reset the connection if the target server has not responded to the request within the allotted time;
  • the server is overloaded and does not have time to serve all clients and return responses to all Nginx requests.

Now let's talk about what you can do and how to fix 504 Gateway Timeout error.

How to fix 504 gateway time out Nginx?

The first thing to try in practice is that if your server, php-fpm or apache lacks system resources, such as memory or CPU, see the free RAM using the free command:

$ free -h

You can find out the CPU load with the htop command:

$ htop

If you see that PHP is taking up all the CPU time, then it means the problem is with the server resources. You can try to deal with the site engine, optimize the resource or choose a powerful VPS server.

The second option - if it was planned so that the script worked for a long time. In this case, you need to configure Nginx to wait for a response from Apache or php-fpm. To solve the problem in the case of php-fpm, you only need to add two lines to the fastgci configuration block:

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

300 means 300 seconds, for most scripts, this is enough, but it is also easy to set more if needed. Also the 504 error can occur when Nginx is used as a proxy for Apache or any other web server, then you need to configure the wait time for the proxy as well.

Add these lines to the server section:

proxy_connect_timeout 600;

proxy_send_timeout 600;

proxy_read_timeout 600;

send_timeout 600;

In this case we have a timeout of 600 seconds.

Now we need to restart Nginx:

$ sudo systemctl restart nginx

Another reason and its solution if the script hangs. If you run the script yourself, you will immediately see what hangs, but if such an error is encountered by users, it is a more serious problem. You can see if your users are encountering such errors and where they occur by using the command:

$ fgrep -i " 504 " /var/log/nginx/access.log

More detailed information can sometimes be seen in error.log:

$ fgrep -i " 504 " /var/log/nginx/error.log

Further, if php-fpm is the problem, you can track which scripts are running slowly using the built-in slow-log function.

To activate it, add the following lines to your pool configuration:

$ sudo vi /etc/php-fpm.d/www.conf
slowlog = /var/log/php-fpm/www-slow.log
request_slowlog_timeout = 5s

This completes the instruction.