With the common use of PHP scripting language, the current Webserice services are mostly in the structure of nginx+ (PHP-FPM), understand the work process before they can do in all aspects of the adjustment optimization and troubleshooting, from the following points to summarize this model.
First, the relationship between Nginx and php-fpm and the division of Labor
Nginxis aWebServer,php-fpmis aphpfastcgiprocess Manager, both followFastCGIprotocol to communicate with theNginxresponsible for static similarHtmlthe processing of documents,php-fpmresponsible forPhpscripting language, so designed to understand the decoupling front endNginxand back end ofPhp, so it's not easy to go wrongPhpthe script blocked the wholeNginxbusiness processes that affect the user experience becausePhpscripting language execution is more prone to problems. NginxThe ability to handle thousands of highly concurrent services, in addition to its own asynchronous nonblocking mode, is inseparable from the coupling extension method with other modules,Nginxdesign is not acceptable is blocking, but not completely sessile, such as the use of the most multi-process single-threaded mode, due toNginxThe log does not have a separate processing process, and if the logs are collected improperly handled, theWorkerprocess to block the dead.
The corresponding NGINX+PHP-FPM model structure diagram is as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/E5/wKioL1fOU-fiLC8nAAEc-FGrRjo932.png "title=" nginx+ PHP-FPM structure model. png "alt=" Wkiol1fou-filc8naaec-fgrrjo932.png "/>
(Fig. 1)
1, Nginx 's work brief (corresponding to Figure 1 see)
After receiving the PHP script request,Nginx passes the request through the fastcgi_pass instruction to the back-end PHP-FPM worker process, In this process,Nginx has made various time-out mechanism, caching mechanism,buffer mechanism and long connection mechanism to ensure that the php-fpm with the back end can cooperate with the benign and efficient.
control over the timeout mechanism Nginx to the back end PHP The waiting time, through various Timeout command, for example:
Fastcgi_connect_timeout back-end link Time
Fastcgi_send_timeout Data Send time, two successful send time difference, not the entire sending times
Fastcgi_read_timeout data receive time, two times successfully received, not the entire receive time
The 504 Timeout status code is returned when the timeout expires, and there are many in the buffer mechanism directive, for example:
Fastcgi_buffer_size Store fastcgi The response header, generally set to paging size
Fastcgi_buffers Store fastcgi Pass the corresponding content, generally set a multiple of the page, format such as 8 4k|8k
In addition, there are some other cache, long connection mechanism does not introduce, when the setting is not reasonable when there will also be 5XX errors, Nginx article has written a lot, no longer do too much explanation.
2, PHP-FPM work introduction (corresponding to Figure 1 see)
PHP-FPM is a phpfastcgi process Manager that will have master and worker two processesafter booting, Master is responsible for receiving external signals and managing worker process,worker process is responsible for the work, handling Nginx sent over the task.
master process only one, responsible for listening ports and managing Worker build 3 The second handshake is put into the connection queue for the worker process Accept worker when there is an error in the process or execution times out, be responsible for Worker The process restarts or kills, is the php-fpm The main body in the model.
worker process is a worker process, each worker program script, and then the results of the execution are passed fastcgi The agreement is given to nginx Master the management. At work, worker process to compete Accept management process master "link queue, accept The function obtains the connection information from the connection request queue, creates a new Socket FD socket for server and Nginx
The PHP-FPM can be configured with multiple pools, all of which are monitored on different ports by Master and assigned to a different worker process pool, theworker Process Pool support dynamic Prefork also support static open, server memory is recommended to configure a static resource pool after the direct calculation, can reduce the overhead of frequent prefork process, improve the quality of service, Because the process model runs more expensive, because each worker process can configure how many requests to reboot, the corresponding pool instructions and the instructions for how many requests are executed are as follows:
PM = Static | Dynamic | OnDemand static pool, service first, memory first
Pm.max_children = Maximum number of PHP processes opened
Pm.max_requests = 1024x768 Restarts the worker process after a request has been executed
This is also our online server configuration, our online Web service machine is a core CPU,16G of memory,nginx open A worker process,php open a process, after running each process about 30M of memory, that is (256+12) *30=8g , also ran some piping, monitoring, statistics, log collection, such as 7788 of the software, the overall business is relatively easy, the configuration of this static pool greatly reduces the cost of prefork process, RT time of 100ms accounted for 85% (This is related to how the program is written), the cost of running a period of time is as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/86/E6/wKiom1fOVyGwRrByAAB0HEoKVdw112.png "title=" Qq20160906134111.png "alt=" Wkiom1fovygwrrbyaab0heokvdw112.png "/>
second, this model structure common 5XX server-side error and optimization (corresponding to Figure 1 see)
1, nginx log generated 502 error
in the first case,when the PHP-FPM worker process executes the PHP program script, the maximum execution time for the configuration is exceeded, andthe master process takes the worker Process is killed and returned directly to 502.
return 502 the error log corresponding to nginx is 104:connection reset by peer
The corresponding php execution time configuration is as follows, some versions of the PHP-FPM configuration will overwrite the php.ini configuration, so that the php.ini configuration does not work:
default 30s in php.ini:max_execution_time =
in php-fpm:request_terminate_timeout =
in the second case, The number of connection requests (before Accpet) exceeds the maximum valueof the TCP connection that the port can listen on ( the backlog value) and cannot be entered in fpm waiting for accept The link queue directly returns 502, where TCP retransmission may occur ;
return 502 after Nginx corresponding error log is 111:connection Refused
The value of the backlog is the sum of the half-connection and the full connection, and his presence also has a short-time buffer to decouple Nginx requests with fpm processing, a half-connection means receiving a syn request,3 The second handshake has not been established, the full connection refers to the 3 times the handshake has been successful, but has not yet been Accpet request,FPM inside has the adjustment parameters, if the FPM parameter is set to 1, The default is to go to the system kernel parameter net.core.somaxconn setting value, if not set can be viewed in /proc/sys/net/core/somaxconn , the default value is 128 , so increase this value in the business where the connection request is high.
502 mainly from the configuration of the php-fpm to consider, according to the server situation, Increase The number of working process of PHP-FPM, increase the execution time of PHP appropriately, increase the backlog value appropriately .
PHP Work Process number is not the bigger the better, this process model run time to occupy the memory will increase, generally a PHP process is accounted for about 30M of memory, open how many suitable for their own,Nginx Worker processes generally can also run to 30M of memory, a comprehensive calculation, PHP execution time can be set according to your service standards, over the service time the browser returned a 502 error, This is handled according to the actual situation, anyway, I think the execution of slow has a return result is better than the direct return of the 502 error, as for the backlog value, when the program is written better, it is recommended to set the number of PHP work process 1 to 2 times.
2, Nginx log generated 504 error
In the first case, the worker process pool of PHP is slow to process, unable to process the link queue waiting for accept as soon as possible , resulting in the link queue after 3 handshake has not been accepted for a long time . nginx link wait timeout;
return 504 after the error log for nginx is 110:connection timed out
In the second case, the backend PHP-FPM executes the script too long, exceeding the time-out mechanism of the nginx configuration, and will also report a 504 error.
504 mainly from the Nginx configuration side to consider, according to business conditions to configure the time-out of various mechanisms, including but not limited to subordinate parameters:
Fastcgi_connect_timeout
Fastcgi_send_timeout
Fastcgi_read_timeout
。。。。。。。。
In addition: in the configuration process, such as encountering large concurrency or a special business scenario, unreasonable FD, buffer and other settings will also bring 5XX errors, such as large concurrent connection to increase the number of systems and a single program of FD, if the upload business to increase the big head buffer, etc. These are to be optimized according to the situation, is the so-called Tao Nature, the operation changed thousands, to status quo.
This article is from the "Running Linux" blog, so be sure to keep this source http://benpaozhe.blog.51cto.com/10239098/1846784
Analysis and optimization of nginx+ (PHP-FPM) structure model of WebService