Nginx, CGI, FastCGI, PHP-CGI, PHP-FPM processing process

Source: Internet
Author: User
: This article mainly introduces Nginx, CGI, FastCGI, PHP-CGI, PHP-FPM processing process, for PHP tutorials interested in students can refer. For cgi fastcgi php-fpm php-cgi, there are many explanations on the Internet, which can be viewed by Baidu. below is my understanding.

Terms:

1. web server 2, Communication Protocol 3, process, main process, sub-process 4, php parser CGI: Common Gateway Interface public Gateway Interface, A standard, interface, protocol for web server and scripting language communications [Protocol] FastCGI: CGI protocol upgraded version [Protocol] PHP-CGI: the CGI interface protocol PHP script parser [program] PHP-FPM: management and scheduling of the php-cgi process, and then the realization of the FastCGI interface protocol program [program]
Webserver can only process static files. it is powerless for dynamic scripts such as php and can only be handled by php itself. Therefore, the following process is available:

However, the above architecture has personality issues. CGI will parse the corresponding script configuration file (such as php. ini), load configuration and expansion, initialize the execution environment, the performance is very poor, all have the following process:

So the implementation of Fastcgi protocol programs, such as PHP-FPM is how to do it? First, Fastcgi will start a master process, parse the configuration file, initialize the execution environment, and then start multiple worker processes. This worker is php-cgi. When the request comes, the master will pass it to a worker and then immediately accept the next request. In this way, repetitive work is avoided, and the efficiency is naturally high. In addition, when the worker is not enough, the master can start several workers in advance according to the configuration, for example, 20 worker. of course, when there are too many idle workers, some will also be stopped, which improves the performance, it also saves resources. This is how fastcgi manages processes.

Below are the configuration items for the number of worker in the php-fpm configuration file:

; The maximum number of processes FPM will fork. This has been design to control; the global number of processes when using dynamic PM within a lot of pools.; Use it with caution.; Note: A value of 0 indicates no limit; Default Value: 0; process.max = 128

The maximum number of worker processes is 128.

More explanations for FastCGI

Fastcgi is an extension based on the cgi architecture. his core idea is to establish an intelligent and sustainable middle layer between the web server and a specific cgi program to manage the running of cgi programs, in this way, the web server only needs to submit requests to this layer, which then generates several reusable cgi program instances and then distributes requests to these instances. these instances are controllable, it is sustainable and reusable. Therefore, on the one hand, it avoids the process repeatedly fork, on the other hand, it can monitor the running status of these instances through the control and detection mechanism of the middle layer, and fork or recycle instances according to different conditions, achieve both flexibility and stability.

References

Https://segmentfault.com/q/1010000000256516

Http://blog.csdn.net/zhuanshenweiliu/article/details/46413241

The above introduced Nginx, CGI, FastCGI, PHP-CGI, PHP-FPM processing process, including the content of the aspect, hope to be interested in PHP Tutorial friends help.

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.