LNMP common 502 Bad Gateway Problems
When we configure LNMP, we always encounter 502 problems. Whether it is in the browser or using the curl command, it is easy to see "502 Bad Gateway ", so what is the cause of the error and how to solve it. I have summarized it based on my experience.
Error 1: configuration error
Let's first introduce the LNMP environment. As the name suggests, the LNMP environment is composed of nginx, mysql, and php. However, the users and groups of nginx are php-fpm, how can we associate nginx with php? There are two modes: Socket mode and TCP/IP mode (or ip: port mode ).
When configuring the nginx virtual host, the configuration file contains the following section:
Location ~ \. Php $ {
Include fastcgi_params;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/data/www $ fastcgi_script_name;
}
Please note that here is the third row of the configuration file. Here I use the ip: port format. When should I use it? Both methods are acceptable. We usually use the socket mode by default, that is, to connect through a socket file, and the default socket file directory is under the/tmp/directory, therefore, the path of the socket file after fastcgi_pass must be correct, otherwise it will certainly be 502 !!
What if the. socket file is not generated in the specified directory? At this time, we need to change fastcgi_pass to the ip: port format. Note that if the content in this configuration file is still 502, we also need to change the content of another configuration file, that is, the php configuration file:/usr/local/php/etc/php-fpm.conf, we open the file to see the following content:
[Global]
Pid =/usr/local/php/var/run/php-fpm.pid
Error_log =/usr/local/php/var/log/php-fpm.log
[Www]
Listen = 127.0.0.1: 9000
User = php-fpm
Group = php-fpm
Listen. owner = nobody
Listen. group = nobody
Pm = dynamic
Pm. max_children = 50
Pm. start_servers = 20
Pm. min_spare_servers = 5
Pm. max_spare_servers = 35
Pm. max_requests = 500
Rlimit_files = 1024
Change the listen part of the configuration file to ip: port, so that nginx and php can be connected and then re-connected.
Load nginx:/etc/init. d/nginx reload
In a word, the form of the two places must be the same. Otherwise, an error will occur.
Error 2: Permission
After starting nginx, you can check the nginx process: ps aux | grep nginx
At this time, we found that the owner and group of the nginx worker process are nobody.
To address the 502 error caused by such a permission issue, we need to in the configuration file/usr/local/php/etc/php-fpm.conf
Add two sentences:
Listen. owner = nobody
Listen. group = nobody
Then reload nginx
Error 3: resource depletion
When the LNMP Architecture processes php, nginx directly calls the backend php-fpm service. If the nginx Request volume is too high, and we have not configured enough sub-processes for php-fpm, when php-fpm resources are used up, once nginx is used up, php-fpm cannot be found, which will cause 502 to appear. The solution at that time is to adjust the pm. max_children value in the php-fpm.conf to increase it. But it cannot be set infinitely. After all, the resources of the server are limited. Based on experience, if a 4 GB memory machine only runs php-fpm and nginx, it does not run mysql service, pm. max_children can be set to 150. Try not to exceed this value. The 8 GB memory can be set to 300, and so on.
Common Errors are the above. If they cannot be solved, please refer to the error log and the error_log in the configuration file nginx. conf to adjust its level to help us see more errors.
Nginx 502 error trigger conditions and Solutions
Install the LAMP \ Vsftpd \ Webmin \ phpMyAdmin service and settings in Ubuntu 13.04
Build and install the LNMP production environment in CentOS 6.4
Practical Production Environment-LNMP architecture compilation and installation + SSL encryption implementation
LNMP full-featured compilation and installation for CentOS 6.3 notes
Install LNMP in CentOS 6.3 (PHP 5.4, MyySQL5.6)
Nginx startup failure occurs during LNMP deployment.
Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)
This article permanently updates the link address: