Problem: the recent flash sales have ignited. When the flash sales arrive, the website may experience a 502 error, which cannot withstand the pressure of consumers.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/56/wKioL1Q7oo-QGbd2AABLUqUU51U912.jpg "Title =" 20110425200117-1568702071.jpg "alt =" wKioL1Q7oo-QGbd2AABLUqUU51U912.jpg "/> injury .....
Previous PHP-FPM Configuration:
For a single PHP-FPM instance, the socket mode and 8 GB static memory mode are used to start a PHP-FPM process with 300 processes. The specific parameters are as follows:
listen = /tmp/php-cgi.sock#listen = 127.0.0.1:9000listen.backlog = 2048listen.allowed_clients = 127.0.0.1pm = staticpm.max_children = 300pm.start_servers = 50pm.min_spare_servers = 30pm.max_spare_servers = 250request_terminate_timeout = 0request_slowlog_timeout = 2
Because of the architecture, code, and other reasons, hundreds of concurrent jobs on a single server may cause 502 errors.
Preliminary solution: various related optimizations,
Increase PM. max_children to 400
Added listen. Backlog = 2048 to nginx and FPM.
Maximum number of opened file handles: 65535
/Etc/sysctl. conf has been fine-tuned. The number of connections initiated by nginx in high concurrency far exceeds the number that PHP-FPM can handle, resulting in frequent locks of ports (or sockets) and congestion. Error 502 still appears
Ultimate solution:
Enable two PHP-FPM instances and divide PHP-FPM into two parts. Each part listens to one port or socket, which reduces the lock and keeps 400 PHP-FPM processes, 200 requests are enabled for each instance. The nginx upstream Server Load balancer is used to poll each socket to process requests.
Specific operations:
CP php-fpm.conf php-fpm2.confvi php-fpm2.conf to make corresponding modifications [Global] pid =/usr/local/PHP/var/run/php-fpm2.piderror_log =/usr/local/PHP/var/log/php-fpm2.loglog_level = notice [www] Listen =/tmp/php-cgi2.sock # Listen = 127.0.0.1: 9000listen. backlog = 2048listen. allowed_clients = 127.0.0.1pm = staticpm. max_children = 200pm. start_servers = 50pm. min_spare_servers = 30pm. max_spare_servers = 250request_terminate_timeout = 0request_slowlog_timeout = 2 slowlog = var/log/slow. logcp/etc/init. d/PHP-FPM/etc/init. d/php-fpm2 VI/etc/init. d/php-fpm2 modify prefix =/usr/local/phpexec_prefix =$ {prefix} php_fpm_bin =$ {exec_prefix}/sbin/php-fpmphp_fpm_CONF =$ {prefix}/etc/php-fpm2.confphp_fpm_PID =$ {prefix }/var/run/php-fpm2.pid
Start php-fpm2
Configure nginx
Edit the main configuration file of nginx. conf. If the backend uses a virtual host,
Add
Upstream backend {
Server UNIX:/tmp/php-cgi.sock;
Server UNIX:/tmp/php-cgi2.sock;
}
VI vhost/test. conf
Modify fastcgi_pass backend here. If FastCGI is called, use Server Load balancer.
Location ~ [^/] \. Php (/| $)
{
Try_files $ uri = 404;
Fastcgi_pass backend;
# Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Include FastCGI. conf;
# Include pathinfo. conf;
}
Restart nginx.
Wait for verification. 502 errors will be greatly reduced, websites will be snapped up, and consumers will enjoy it.
Summary:
When high concurrency occurs, the method of using TCP ports is relatively more stable than that of socket. However, the efficiency of using ports is indeed less efficient than that of socket. In the lnmp environment, in the face of high concurrency, in addition to a reasonable architecture and reasonable optimization, the developer's code logic and efficient code are also an important factor affecting high concurrency. How many PHP-FPM requests are called and how long each PHP-FPM processes are considered by developers.
This article from the "Chapter ainemo _ Linux" blog, please be sure to keep this source http://zhangxylinux.blog.51cto.com/5041623/1563427
How to deal with you-lnmp high concurrency 502