Home  >  Article  >  Backend Development  >  nginx timeout

nginx timeout

WBOY
WBOYOriginal
2016-08-08 09:31:061068browse

Recently, 504 Gateway Time-out always appears when the website is processing big data, so I found some information on the Internet

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but due to some reason (generally It is a problem of reading resources) that has not been completed and caused the PHP-CGI process to terminate.

 Nginx 504 Gateway Time-out means that the requested gateway has not been requested. Simply put, it means that no executable PHP-CGI has been requested.

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

 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 Pen4 1.5G, the memory is 1GB, and it is a CENTOS system. There are about 50 visitors online at the same time.

 But most people online 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;

 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; gi_connect_timeout 300s;

 fastcgi_send_timeout 300s;

 fastcgi_read_timeout 300s;

This stipulates the time for connecting, sending and reading of PHP-CGI. 300 seconds is enough, so my server rarely encounters the error 504 Gateway Time-out. The most critical thing is the setting of php-fpm.conf, which will directly lead to 502 Bad Gateway and 504 Gateway Time-out.

 Now let’s 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"

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

 The calculation method is as follows:

 If your server performance is good enough, and the broadband resources are sufficient, and there are no loops or bugs in the PHP script, 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 bandwidth is not sufficient, or other reasons cause your PHP-CGI to be suspended, then it is recommended that you assign "request_terminate_timeout" A value that can be set based on the performance of your server. Generally speaking, the better the performance, the higher you can set it, anywhere from 20 minutes to 30 minutes. Since my server PHP scripts need to run for a long time, some may take more than 10 minutes, so I set 900 seconds so that PHP-CGI will not die and a 502 will appear. Bad gateway error.

And how is the value of "max_children" calculated? In principle, the bigger the value, the better. If there are more php-cgi processes, it will be processed quickly and there will be fewer queued requests. Setting "max_children" also needs to be set according to the performance of the server. Generally speaking, the memory consumed by each php-cgi on a server is about 20M under normal circumstances, so I set my "max_children" to 40, 20M* 40=800M means that at the peak time, the memory consumed by all PHP-CGI is within 800M, which is 1Gb lower than my effective memory. And if my "max_children" is set to a smaller value, such as 5-10, then php-cgi will be "very tired", the processing speed will be very slow, and the waiting time will Also longer. If a request has not been processed for a long time, the error 504 Gateway Time-out will appear. If the php-cgi that is being processed very tiredly encounters a problem, the error 502 Bad gateway will appear.

fastcgi_read_timeout 300s;

The above introduces nginx timeout, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn