nginx+php (Fastcgi) Common 502 and 504 solution ideas sharing

Source: Internet
Author: User
Tags connection reset

Company A test server, the recent 504 and 502 problems: ( environment lnmp,php compiled 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, analysis of the problem:

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

The implication of Nginx 502 Bad Gateway is that the requested php-cgi has been executed, but the php-cgi process is terminated because the read resource has not been executed.

Second, the Troubleshooting steps:

1) 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 off) while reading response ...

2.1.2) PHP related logs

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; #指定连接到后端FastCGI   Time-out. fastcgi_send_timeout 60; #向FastCGI   Transfer Request Timeout, which is the time-out for sending a request to fastcgi  after two handshake has been completed. fastcgi_read_timeout 300; #接收FastCGI   Response Timeout, which is the time-out for receiving fastcgi  answers 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; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI   's response. fastcgi_busy_buffers_size 8k; is twice times #默认值是fastcgi_buffers  . fastcgi_temp_file_write_size 8k; the number of blocks that will be used #在写入fastcgi_temp_path  , the default value is twice times the fastcgi_buffers . fastcgi_cache test# Open the fastcgi  cache and set a name for it. fastcgi_cache_valid 200 302 1h;fastcgi_cache_valid 301 1d;fastcgi_cache_valid  any 1m; #为指定的应答代码指定缓存时间, as in the previous example, 200,302  answer cache for one hour,301  answer cache 1  days, others 1  minutes. fastcgi_cache_min_uses 1; #缓存在fastcgi_cache_path   Directive inactive  the minimum number of usage times for the parameter value time, as in the example above, if a file is 1 in 5  minutes   times are not used, then this file will be removed.

3.1.2) Viewing 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 the previous experience 300s enough, The most important setting is the first three articles:

Fastcgi_connect_timeout 300s;fastcgi_send_timeout 300s;fastcgi_read_timeout 300s;

3.1.4) to tune PHP parameters:

Ini

Max_execution_time = 60

Php-fpm.conf:

request_terminate_timeout=300s# default is 0

3.1.4.1) The above parameters function

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 executed the script. Therefore, Nginx found that the connection with their own communication has been broken, it will return to the client 502 error.

3.1.4.2) Parameters specific configuration method:

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

"Max_children" also need to be based on the performance of the server set, in general, a server under normal circumstances each php-cgi consumes about 20M of memory, can be defined according to their own configuration.


3.1.5) 502 and 504 other issues that may exist

3.1.5.1) Nginx's max_fail,fail_timeout problem

3.1.5.2) Network Occasional problem

location ~ \.php$ {                 root                     htdocs;                 include                  fastcgi_params;                 fastcgi_connect_timeout          60;                 fastcgi_read_timeout            120;                 fastcgi_send_timeout              120;} 

Three, Summary:

4.1) according to Ninx related error can be directly based on common error targeting target.

4.2) quickly lock the cause according to the log. The log is the wrong magic weapon, must make full use of.

4.3) Adjust the parameters to note the backup of the file, avoid misoperation. Also take into account the issue of the priority of parameter entry.

4.4) The problem summary, make notes, the next encounter can be quickly resolved.

This article is from "Never give up!" Ningzhiyuan "blog, be sure to keep this source http://renzhiyuan.blog.51cto.com/10433137/1883961

nginx+php (Fastcgi) Common 502 and 504 solution ideas sharing

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.