Perfect solution to Nginx 504 Gateway time out problem

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

Recently built dedecms 1 site added a large number of content, there are 3 columns within capacity than the two same, the two columns in the generation of more than two thousand 300 columns list in 504 when Gateway time - out nginx server, I am not very understand, server maintenance personnel literally sought 1 article online, modified nginx cache Settings, no matter use, he would have no matter, but I can't no matter ah, can not generate the list page, that the back of the content is not can't use it & # 63;

The database was downloaded to the local, in the local configuration of nginx, tried many times, but it did not work, and then changed to Apache, more exaggerated, generated 83 pages can not continue, it seems that nginx is more powerful 1 point, although 504 Gateway time-out, but it can be generated.

Had to continue to look for more solutions on the Internet, N tried many times, finally let me find a useful method, think about the future may still encounter such a problem, I will copy the article over here for future reference, to have the same problem of a friend is also a help.

              the following part is the quote part, I don't know the technology myself, it is useful in my dedecms 5.6, please try it by yourself.

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process has been terminated due to some reason (1 is usually a reading resource issue) not completing the execution.

Nginx 504 Gateway Time-out means that the requested gateway has not been requested. In short, PHP-CGI has not been requested and can be executed.

Generally speaking, Nginx 502 Bad Gateway is related to php-fpm. conf, while Nginx 504 Time-out is related to nginx. conf.

The correct setup takes into account multiple factors such as the performance of the server itself and the number of visitors.

Take my current server as an example, CPU is running at 41.5G, memory 1GB, CENTOS system, visitors are about 50 people online at the same time.

However, most people online need to request PHP-CGI for a lot of information processing, so I set nginx. conf as:

300 s fastcgi_connect_timeout;

300 s fastcgi_send_timeout;

300 s fastcgi_read_timeout;

128 k fastcgi_buffer_size;

128 k fastcgi_buffers 8; # 8, 128

256 k fastcgi_busy_buffers_size;

256 k fastcgi_temp_file_write_size;

fastcgi_intercept_errors on;

The main Settings here are the first three, namely

300 s fastcgi_connect_timeout;

300 s fastcgi_send_timeout;

300 s fastcgi_read_timeout;

The time required to connect, send, and read for PHP-CGI is specified here, and 300 seconds is sufficient, so my server rarely has the error of 504 Gateway Time-out. The most important thing is the setting of php-fpm.conf, which leads directly to 502 Bad Gateway and 504 Gateway Time-out.

Below, we will carefully analyze several important parameters of php-fpm.conf:

php-fpm.conf has two crucial parameters, one is "max_children" and the other is "request_terminate_timeout".

One of my two Settings has a value of "40" and one of "900", but this value is not universal and needs to be calculated by myself.

The calculation method is as follows:

If your server is performing well and your broadband resources are sufficient, you can set "request_terminate_timeout" to 0s if the PHP script does not have a loop or BUG. The meaning of 0s is to allow PHP-CGI1 to proceed without a time limit. And if you can't do that, which means your PHP-CGI might have some BUG, or you don't have enough bandwidth or something that causes your PHP-CGI to fake death, then it's recommended that you assign a value to "request_terminate_timeout," which can be set based on the performance of your server. Generally speaking, the better the performance is, the higher you can set it, from 20 minutes to 30 minutes. Since my server PHP script takes a long time to run, some of it may take more than 10 minutes, so I set 900 seconds so that PHP-CGI does not die and the error of 502 Bad gateway occurs.

And how do you calculate "max_children"? In principle, the larger the value, the better. The more processes php-cgi have, the faster they will be processed and the fewer requests will be queued. The setting of "max_children" also needs to be set according to the performance of the server. Generally speaking, the memory consumed by each php_cgi of one server is about 20M under normal circumstances, so I set my "max_children" to 40, and 20M*40=800M, which means that the memory consumed by all PHP-CGI is within 800M at the peak, which is lower than my effective memory 1Gb. If my "max_children" is set to a smaller number, say 5-10, then php-cgi will be "very tired", slow to process and have a longer wait time. If a request has not been processed for a long time, the error will appear: 504 Gateway Time-out, while the tired php-cgi will appear: 502 Bad gateway if there is a problem.


Related articles: