[Go] How do you face-lnmp high concurrency when 502

Source: Internet
Author: User

From:http://www.topthink.com/topic/5683.html

Before PHP-FPM configuration:

Single PHP-FPM instance, using socket mode, memory 8G static mode, start PHP-FPM process number 300, 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=StaticPmmax_children = 300 pm.= 50pm min_spare_servers = 30pm.= 250= 0 request_slowlog_timeout = 2  


Because of the schema, code, and so on, a single hundreds of concurrent 502 error occurred.

Preliminary solution: Various related optimizations

Increase Pm.max_children to 400

Nginx and fpm Added Listen.backlog = 2048

Maximum open file handle 65535

/etc/sysctl.conf have been fine-tuned, high concurrency when Nginx initiated the number of connections, far more than the number of PHP-FPM can handle, causing the port (or socket) is frequently locked, causing congestion. 502 Errors still occur

The ultimate Solution:

Enable two PHP-FPM instances, the php-fpm divided into two parts, each listening to a port or socket, which reduces lock, still maintain 400 php-fpm process, 200 per instance, with Nginx upstream load balancing, Poll each socket to process the request.

Specific operation:

CP PHP-Fpm.conf PHP-Fpm2.Confvi PHP-Fpm2.ConfMake the appropriate changes[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=2Slowlog=Var/Log/Slow.Logcp/etc/Init.D/Php-Fpm/etc/Init.D/Php-FPM2 VI/etc/Init.D/Php-Fpm2ModifyPrefix=/usr/Local/Phpexec_prefix=${Prefix}Php_fpm_bin=${exec_prefix}/sbin< Span class= "pun" >/php-fpmphp_fpm_conf=< Span class= "PLN" >${prefix}/etc /php-fpm2.=${prefix }/var/run/ php-fpm2. PID                  


Start PHP-FPM2 and then

Configure Nginx

Edit the nginx.conf master configuration file, if the backend uses a virtual host, as I do,

Add to

Upstream Backend{              server  unix:/tmp/php-. Sock;               server unix :/tmp/php-cgi2sock;}vi vhost/testconf               

The

Modify here  fastcgi_pass  backend;  call fastcgi is a way to use load balancing.

location ~  [^/]\.php (/| $  { Try_files $uri =< Span class= "lit" >404; Fastcgi_pass backend; # fastcgi_pass 127.0.0.1:9000;  Fastcgi_index index.; include Fastcgi.; # include pathinfo.conf;  }            /span>                



Restart Nginx.

Wait for verification, the 502 error will be greatly reduced, the website snapped, the consumer very happy.

Summarize:

TCP ports are relatively stable in high concurrency, but using ports is a bit less efficient than the socket. LNMP environment, in the face of high concurrency, in addition to a reasonable architecture, and reasonable tuning, the developer's code logic and efficient code is also an important factor affecting high concurrency. How many times a request is called PHP-FPM, and how much time each PHP-FPM handles is a point that developers need to consider.

[Go] How do you face-lnmp high concurrency when 502

Related Article

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.