The solution to the 502 problem reported by php-fpm


After building the lnmp environment, a 502 error occurred during the test. When I saw this problem, I immediately thought that php-fpm was not up, but I usedps -ef | grep php-fpmI intercepted the process of php-fpm and found that there is. At this time, I checked the error log of nginx and found that the error message is:

2017/05/05 17:08:45 [crit] 3258#0: *2 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) while connecting to upstream, client:, server:, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://unix:/tmp/php-cgi.sock:”, host: “”

Remarks: The version of php I use is 7.1.4, the latest version. This problem did not occur when I used version 5.6 before.

Idea: It is found that the listen of php-fpm runs in unix sock mode, and the problem may lie in php-fpm. Since there is no such file, create the file first
1. Create this file and give permission, because I use www user when compiling, so authorize the owner and group of /tmp/php-cgi.sock to www

touch /tmp/php-cgi.sock

chown www.www /tmp/php-cgi.sock

Restart ngix and php-fpm and find that the problem is still 502

2. I also checked on the Internet and said it was one of them
fastcgi_pass is to configure the interaction path between nginx and php-fpm, there are generally two ways
Sock mode: fastcgi_pass Unix :/ TMP/PHP-cgi.sock;
http method: fastcgi_pass;
You can choose one of them, but it must be consistent with the configuration of php-fpm.
Later, I added the following code to the nginx configuration file

location ~\.php$ {  

    root /root/wwwroot;  

    fastcgi_pass unix:/tmp/php-cgi.sock;  

    fastcgi_index index.php;  

    include fastcgi.conf;  

3. At the same time, modify the configuration file php-fpm.conf of php-fpm and change listen = to

listen = /tmp/php-cgi.sock
#This is for setting /tmp/php-cgi.sock permissions
listen.owner = www = www
listen.mode = 0660

Restart ngix and php-fpm again, the problem is solved


1. In fact, this problem is generally the interaction between nginx and php-fpm. It depends on whether we choose the sock method or the http method, but no matter which one we choose, we need to unify
2. I saw on the Internet that someone said to increase the max_children process. In fact, I have encountered this situation before. It is suitable for the original php running normally, but the problem of 502 suddenly appears. This problem is that the php-fpm queue is full and appears Connection refused error. If you have any questions, you can refer to my blog:
3. Some people say that there is a backlog in the php-fpm configuration file. The backlog is the definition of the number of connections processed by the linux server in the socket. The default value of phpfpm is -1, and -1 is changed to 4096. Restart php-fpm.
4. In fact, no matter which one we choose, it is mainly for our own problems. First, we should look at the error report, and then prescribe the right medicine. Don’t try this kind of thing.

The above is the detailed content of the solution to the php-fpm report 502 problem. For more information about the php-fpm report 502 problem, please pay attention to other related articles on developpaer!