Perfectly solve Nginx 504 Gateway time-out

  
                  

A website recently built with dedecms has added a large amount of content. There are three columns with more than two thousand content. More than two hundred and three hundred columns have 504 Gateway when generating the column list. Time-out server uses nginx, I don't understand very well, server maintenance personnel randomly find an article on the Internet, modify the nginx cache settings, no matter what, he does not care, but I can not ignore, can not generate a list The page, the content behind it can not be used?

Download the database to the local, configure nginx locally, tried many times, will not work, and changed to Apache below, more exaggerated, generated 83 pages can not continue, it seems that nginx is even more powerful, although there is 504 Gateway time-out, but it can be fully generated.

I have to continue to find more solutions on the Internet. After trying N many times, I finally found a useful method. I think that I might have encountered such a problem in the future. The article is copied here for future reference, and it is also helpful for friends who have the same problem.

The following part is the reference part. I don't know the technology myself. It is useful in my dedecms5.6. Try it yourself.


Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but for some reason (generally reading the resource) the execution of the PHP-CGI process is terminated.

Nginx 504 Gateway The meaning of Time-out is that the requested gateway does not request it. Simply put, there is no request for PHP-CGI to be executed.

Solving these two problems actually requires a comprehensive thinking. Generally speaking, Nginx 502 Bad Gateway is related to the setting of php-fpm.conf, while Nginx 504 Gateway Time-out is set with nginx.conf. related.

And the correct settings need to consider multiple factors such as the performance of the server itself and the number of visitors.

Taking my current server as an example, the CPU is a rushing four 1.5G, 1GB of memory, CENTOS system, visitors are about 50 people online at the same time.

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

fastcgi_connect_timeout 300s;

fastcgi_send_timeout 300s; Br>

fastcgi_read_timeout 300s;

fastcgi_buffer_size 128k;

fastcgi_buffers 8 128k;#8 128

fastcgi_busy_buffers_size 256k;

fastcgi_temp_file_write_size 256k;

fastcgi_intercept_errors on;

The most important settings here are the first three, namely

fastcgi_connect_timeout 300s;

fastcgi_send_timeout 300s;

fastcgi_read_timeout 300s;

This specifies the time for PHP-CGI to connect, send, and read. 300 seconds is enough, so my server rarely has the 504 Gateway Time-out error. The most critical is the php-fpm.conf setting, which directly leads to 502 Bad Gateway and 504 Gateway Time-out.

Let's take a closer look at some important parameters of php-fpm.conf:

php-fpm.conf has two crucial parameters, one is "max_children", another One is "request_terminate_timeout"

The values ​​of my two settings are "40" and one is "900", but this value is not universal, but needs to be calculated by myself.

The calculation is as follows:

If your server performance is good enough and the bandwidth resources are sufficient, PHP scripts are not looping or buggy, you can directly set "request_terminate_timeout" to 0s. The meaning of 0s is to let PHP-CGI continue to execute without time limit. And if you can't do this, that is to say, your PHP-CGI may have a bug, or your broadband is not enough or other reasons cause your PHP-CGI to be suspended, then you are advised to give "request_terminate_timeout" A value that can be set based on the performance of your server. In general, the better the performance, the higher you can set, 20 minutes to 30 minutes. Since my server PHP script needs to run for a long time, some may exceed 10 minutes, so I set 900 seconds, which will not cause PHP-CGI to die and 502 Bad gateway error.

And how is the value of "max_children" calculated? In principle, the larger the better, the faster the php-cgi process will be processed, and the queued requests will be very small. . Setting "max_children" also needs to be set according to the performance of the server. Generally speaking, the normal memory consumption of a php-cgi is about 20M, so my "max_children" is set to 40, 20M*. 40=800M means that at the peak, all PHP-CGI consumes less than 800M, which is lower than my effective memory of 1Gb. And if my "max_children" is set smaller, such as 5-10, then php-cgi will be "very tired", the processing speed is very slow, and the waiting time is longer. If the request is not processed for a long time, the 504 Gateway Time-out error will occur, and the php-cgi that is being processed very tired will have the error 502 Bad gateway if there is a problem.

Copyright © Windows knowledge All Rights Reserved