504 gateway time-out
Problem:
The requested gateway does not have a request. In short, it does not have a request for PHP-CGI that can be executed.
In general, this situation may be caused by the small buffer of nginx's default FastCGI process response, which will cause the FastCGI process to be suspended. If your FastCGI service does not handle this suspension well, in the end, 504 gateway time-out is likely to occur.
Solution:
It depends on the nginx. conf settings.
The default FastCGI Process Response Buffer is 8 KB. We can set a large value.
In nginx. conf, add:
Fastcgi_buffers 8 128 K
This indicates that the FastCGI buffer is set to 8*128 K.
Of course, if you are performing an immediate operation, you may need to increase the value of the nginx timeout parameter, for example, to 60 seconds:
Send_timeout 60;
Taking my current server as an example, the CPU runs for 4 Gbps, with 1 GB of memory and centos. The number of visitors is about 50 online simultaneously.
However, most people online need to request a PHP-CGI for a lot of information processing, so I set nginx. conf:
Fastcgi_connect_timeout 300 s;
Fastcgi_send_timeout 300 s;
Fastcgi_read_timeout 300 s;
Fastcgi_buffer_size 128 K;
Fastcgi_buffers 8 128 K; #8 128
Fastcgi_busy_buffers_size 256 K;
Fastcgi_temp_file_write_size 256 K;
Fastcgi_intercept_errors on;
The most important setting here is the first three, that is
Fastcgi_connect_timeout 300 s;
Fastcgi_send_timeout 300 s;
Fastcgi_read_timeout 300 s;
This specifies the connection, send, and read time for the PHP-CGI, 300 seconds is enough, so my server rarely gets the 504 gateway time-out error.
Ngnix 502 Bad Gateway
Problem:
The requested PHP-CGI has been executed, but the PHP-CGI process terminated due to some reason (generally a problem reading the resource) not completed.
Solution:
It is related to the settings for the php-fpm.conf.
The php-fpm.conf has two crucial parameters: "max_children" and "request_terminate_timeout"
One of my two values is "40" and the other is "900", but this value is not general, but needs to be calculated by myself.
The calculation method is as follows:
If your server has good performance and sufficient bandwidth resources, you can directly set "request_terminate_timeout" to 0 s if the PHP script does not have loops or bugs. 0 s means to keep the PHP-CGI running without a time limit. And if you can't do that, that is, your PHP-CGI may have a bug, or if your bandwidth is insufficient or your PHP-CGI may be suspended for other reasons, we recommend that you assign a value to "request_terminate_timeout", which can be set based on the performance of your server. In general, the better the performance, the higher you can set, 20 minutes-30 minutes can be done. Since my server PHP script takes a long time to run, some may take more than 10 minutes, so I set 900 seconds, this will not cause the PHP-CGI to die and the 502 Bad Gateway error occurs.
How is the value of "max_children" calculated? In principle, the larger the value is, the better. If the PHP-CGI process is too large, it will process very quickly and there will be very few requests in the queue. "Max_children" also needs to be set based on the server performance. Generally, a server normally consumes about 20 mb of memory per PHP-CGI, so my "max_children" I set to 40, 20 m * 40 = 800m that is to say that at the peak of all PHP-CGI consumption exists within M, less than my Effective Memory 1 GB. If my "max_children" settings are small, such as 5-10, PHP-CGI will be "very tired", the processing speed will be slow, and the waiting time will be long. If a request is not processed for a long time, the error 504 gateway time-out occurs, if the PHP-CGI that is working very well encounters a problem, the 502 Bad Gateway error will occur.
Resolve 504 gateway time-out and 502 Bad Gateway (nginx)