nginx+php (Fastcgi) Common 502 and 504 problem resolution

Source: Internet
Author: User
Tags fpm php script terminates connection reset time limit

Reprint article Source: http://www.linuxidc.com/Linux/2017-02/140777.htm (if invasion delete)

The full text reads as follows:

Company a test server, recently appeared 504 and 502 Problems: (Environment lnmp,php compile installation, fastcgi mode), the problem is not difficult to solve, but here to share their own troubleshooting ideas and ways to deal with the problem.

504 Gateway Time-out,nginx 502 Bad Gateway

First, the analysis of the problem:

The meaning of Nginx 504 Gateway time-out is that there is no request to the php-cgi that can be executed.

The meaning of Nginx 502 Bad Gateway is that the requested php-cgi has been executed, but the php-cgi process terminates due to the lack of execution of the reading resource.

Second, the Troubleshooting steps:

1) to see if Nginx can parse the PHP file normally (parse normal)

2 View nginx access log and PHP log (log error, lock problem)

2.1.1) Nginx Related log

Failed (104:connection reset by peer) while reading ...
Timed out (110:connection timed out) while reading response ...

2.1.2) PHP related log

Warning:child 25718 exited on signal (sigterm) after 21008.883410 seconds from start

3 View PHP and Nginx related configuration (fastcgi related parameters)

......

3.1.1) Nginx and fastcgi commonly used related parameters:

Fastcgi_connect_timeout 60;

The timeout time for the #指定连接到后端FastCGI.

Fastcgi_send_timeout 60;

#向FastCGI timeout for the transfer request, which is the timeout for sending requests to fastcgi after two handshakes have been completed.

Fastcgi_read_timeout 300;

The timeout for the #接收FastCGI answer, which is the timeout for receiving fastcgi responses after two handshakes have been completed.

Fastcgi_buffer_size 4k;

#指定读取FastCGI answer the first part of the need to use how much buffer, generally the first part of the answer will not exceed 1k, because the page size is 4k, so this is set to 4k.

Fastcgi_buffers 8 4k;

A #指定本地需要用多少和多大的缓冲区来缓冲FastCGI answer.

Fastcgi_busy_buffers_size 8k;

Twice times the #默认值是fastcgi_buffers.

Fastcgi_temp_file_write_size 8k;

How large a block of data will be used when #在写入fastcgi_temp_path, and the default value is twice times fastcgi_buffers.

Fastcgi_cache TEST

#开启FastCGI cache and make a name for it.

Fastcgi_cache_valid 302 1h;
Fastcgi_cache_valid 1d;
Fastcgi_cache_valid any 1m;

#为指定的应答代码指定缓存时间, as in the example above, the 200,302 answer is cached for one hour, 301 responses are cached for 1 days, and the other is 1 minutes.

Fastcgi_cache_min_uses 1;

#缓存在fastcgi_cache_path instruction inactive the minimum number of times in the parameter value time, as in the above example, if a file is not used 1 times within 5 minutes, the file will be removed.

3.1.2) View Process usage

Netstat-autpn|grep "php-cgi" |wc-l

3.1.3) View Nginx and fastcgi related configurations

Fastcgi_connect_timeout 60;
Fastcgi_send_timeout 60;
Fastcgi_read_timeout 60;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;

Adjust the time, according to previous experience 300s enough, the main setting is the first three:

Fastcgi_connect_timeout 300s;
Fastcgi_send_timeout 300s;
Fastcgi_read_timeout 300s;

3.1.4) to adjust the PHP related parameters:

Ini

1 Max_execution_time = 60

Php-fpm.conf:

request_terminate_timeout=300s
#默认是0

3.1.4.1) The above parameter action

Both are used to configure the maximum execution time for PHP scripts. When this time is exceeded, php-fpm not only terminates the execution of the script, but also terminates the worker process that executes the script. Therefore, Nginx found that the connection to its own communication broke down, it will return to the client 502 error.

3.1.4.2) Parameter specific configuration method:

If the server performance is good enough and the broadband resources are sufficient, the PHP script does not have a loop or bug, you can set the "request_terminate_timeout" to 0s directly. The meaning of 0s is to allow php-cgi to carry on without any time limit.

"Max_children" also need to set according to the performance of the server, generally a server under normal circumstances of each php-cgi memory consumed in about 20M, according to their own configuration specific definition.

3.1.5) 502 and 504 other problems that might exist

3.1.5.1) The max_fail,fail_timeout problem of Nginx

3.1.5.2) Network accidental problem

3.1.5.3) Parameter configuration conflict problem (Eg:php.ini and Php-fpm,nginx configuration fastcgi specified relevant parameters)

Location ~ \.php$ {
Root Htdocs;
Include Fastcgi_params;
Fastcgi_connect_timeout 60;
Fastcgi_read_timeout 120;
Fastcgi_send_timeout 120;
}

Third, Summary:

4.1 According to Ninx related error can be directly based on the common error to lock the target.

4.2 According to the log, quickly lock the cause. Log is the Magic weapon of error, must make full use of.

4.3 parameter note The file backup, avoid misoperation. In addition, consider the issue of priority for parameter entry.

4.4 Problem Summary, good notes, the next encounter can be quickly resolved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.