nginx 504 Gateway Time out error resolution

  • 2020-05-09 19:51:49
  • OfStack

In general, this situation may be due to the fact that the nginx default fastcgi process responds to a buffer that is too small, which will cause the fastcgi process to be suspended. If your fastcgi service does not handle this suspension well, then it will most likely result in 504 Gateway Time-out
Now the website, especially some forums have a large number of replies and a lot of content, a page even has several hundred K
The default buffer for the fastcgi process response is 8K, which we can make larger

In nginx.conf, add:

fastcgi_buffers 8 128k

This means setting the fastcgi buffer to 8×128k
Of course, if you are doing an immediate operation, you may need to increase the timeout parameter of nginx, for example, to 60 seconds:

send_timeout 60;

I just adjusted these two parameters, and the result was that I didn't show that timeout anymore, which is fine

Another article

The first is to change several configurations of php-fpm:

Change max_children from 10 to 30 to make sure there are enough php-cgi processes available.

Change request_terminate_timeout from the previous 0s to 60s, so that the timeout time of the php-cgi process script is 60 seconds, which can prevent all processes from being suspended and improve utilization efficiency.

Then change several configuration items of nginx to reduce the number of requests of FastCGI and try to keep buffers unchanged:

fastcgi_buffers changed from 4 64k to 2 256k;
fastcgi_buffer_size changed from 64k to 128K;
fastcgi_busy_buffers_size changed from 128K to 256K;
fastcgi_temp_file_write_size changed from 128K to 256K.

Ok, reload the configuration of php-fpm and nginx, and test it again. So far, there has been no 504 Gateway Time-out in two weeks, which is considered to have achieved the effect.

In addition, the default static processing mode of php-fpm will make the process of php-cgi occupy memory for a long time and cannot be released, which is one of the reasons for the error of nginx. Therefore, the processing mode of php-fpm can be changed to apache mode.
apache-like


Related articles: