Nginx+PHP (Fastcgi) common 502 and 504 problem solving

One of the company's test servers has recently encountered 504 and 502 problems: (Environment LNMP, php compilation and installation, Fastcgi mode), the problem is not difficult to solve, but here I share my own troubleshooting ideas and ways to deal with the problem.

504 Gateway Time-out,nginx 502 bad gateway

1. Analysis of the problem:

The meaning of Nginx 504 Gateway Time-out is that there is no request to the executable PHP-CGI.

The meaning 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.

2. Checking steps:

1) Check if Nginx can parse the php file normally (parsing is normal)

2) View Nginx access log and php log (log error, locking problem)

2.1.1) Nginx related logs

 failed (104: Connection reset by peer) while reading... 
 timed out (110: Connection timed out) while reading response...

2.1.2) php related logs

WARNING:  child 25718 exited on signal 15 (SIGTERM) after 21008.883410 seconds from start

3) View php and nginx related configuration (Fastcgi related parameters)

......

3.1.1) Common related parameters of Nginx and Fastcgi:

fastcgi_connect_timeout 60; 
  
#Specify the timeout for connecting to the backend FastCGI. 
  
fastcgi_send_timeout 60; 
  
#Timeout for sending requests to FastCGI, this value refers to the timeout for sending requests to FastCGI after two handshakes have been completed. 
  
fastcgi_read_timeout 300; #Timeout for 
  
receiving FastCGI response, this value refers to the timeout for receiving FastCGI response after two handshakes have been completed. 
  
fastcgi_buffer_size 4k; #Specify 
  
the size of the buffer needed to read the first part of the FastCGI response. Generally, the first part of the response will not exceed 1k. Since the page size is 4k, it is set to 4k here. 
  
fastcgi_buffers 8 4k; #Specify 
  
how many and how many buffers are needed locally to buffer FastCGI responses. 
  
fastcgi_busy_buffers_size 8k; #The 
  
default value is twice the size of fastcgi_buffers. 
  
fastcgi_temp_file_write_size 8k; 
  
#How much data block will be used when writing fastcgi_temp_path, the default value is twice the fastcgi_buffers. 
  
fastcgi_cache TEST #Open 
  
FastCGI cache and give it a name. 
  
fastcgi_cache_valid 200 302 1h; 
fastcgi_cache_valid 301 1d; 
fastcgi_cache_valid any 1m; 
  
#Specify the cache time for the specified response code. In the above example, the 200 and 302 responses are cached for one hour, the 301 responses are cached for one day, and the others are cached for one minute. 
  
fastcgi_cache_min_uses 1; 
  
#The minimum number of times cached within the inactive parameter value of the fastcgi_cache_path directive. For example, if a file is not used once 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 configuration

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 is enough, the most important settings are the first three:

fastcgi_connect_timeout 300s; 
fastcgi_send_timeout 300s; 
fastcgi_read_timeout 300s;

3.1.4) Adjust php related parameters:

php.ini

1 max_execution_time = 60

php-fpm.conf:

request_terminate_timeout=300s #The 
default is 0

3.1.4.1)上述参数作用

两项都是用来配置PHP脚本的最大执行时间的。当超过这个时间时,PHP-FPM不只会终止脚本的执行,还会终止执行脚本的Worker进程。因此Nginx发现与自己通信的连接断掉了,就会返回给客户端502错误。

3.1.4.2)参数具体配置方式:

  如果服务器性能足够好,且宽带资源足够充足,PHP脚本没有系循环或BUG的话你可以直接将”request_terminate_timeout”设置成0s。0s的含义是让PHP-CGI一直执行下去而没有时间限制。

  ”max_children”也需要根据服务器的性能进行设定,一般来说一台服务器正常情况下每一个php-cgi所耗费的内存在20M左右,可根据自己的配置具体定义。

3.1.5)502和504可能存在的其它问题

3.1.5.1)Nginx的max_fail,fail_timeout问题

3.1.5.2)网络偶然问题

3.1.5.3)参数配置冲突问题(eg:php.ini和php-fpm,Nginx配置Fastcgi指定了相关参数)

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

三、总结:

4.1)根据Ninx相关报错可直接根据常见报错锁定目标。

4.2)根据日志,快速锁定原因。日志是排错的法宝,一定要充分利用。

4.3)调参数注意文件的备份,避免误操作。此外要考虑到参数生效优先级的问题。

4.4)问题总结,做好笔记,下次遇到则可快速解决。

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx的500,502,504错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

Nginx提示504 Gateway Time-out错误的解决方法 http://www.linuxidc.com/Linux/2017-01/139764.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324722626&siteId=291194637