Nginx the process of parsing a PHP script ____php

Source: Internet
Author: User
Tags fpm php language php script

Some PHP-FPM problems have been encountered in the previous project: Because some of the site's interface, in particular cases of slow response (1-2 minutes), the delay does not end, resulting in more and more php-fpm, and finally reached 127, the server did not respond to the normal request, the browser has been spinning, delayed response. After the interface is repaired, the number of PHP-FPM processes is stabilized and the problem is solved. This has made me interested in the process of PHP-FPM.


In Baidu and Google a lot of articles, I summed up about the concept of cgi,fastcgi,php-fpm,php-cgi, as well as the process of Nginx parsing PHP, as follows: (Bo Master Knowledge level is limited, if there is a wrong place, you are welcome to correct)


1. What is CGI
CGI is the abbreviation of Common Gateway Interface (Public Gateway Interface), what's the use of it? We know that the browser accesses a page that contains dynamic data, and the browser sends the request using the HTTP protocol. The packet passes through the router and the switch, arrives the target Web server, the Web server receives this request, needs and the PHP communication, can obtain the page which contains the dynamic data. Why to communicate. Because of their language impassability (Nginx and other Web servers and PHP language and syntax is not the same, can not communicate directly)

So, how do you communicate? The first is to use the CGI protocol to communicate. With the CGI protocol, Nginx is able to translate HTTP requests into languages that can be understood by PHP (Uri,post data and get data, HTTP headers, and so on). CGI sets out what data to transmit and in what format it is passed to the rear.


1, the traditional use of CGI protocol working mode
The Web server itself is just a distributor of content, such as requesting the/index.html,web server to look for the file and return it to the browser, which refers only to static files. What would happen if it was a request/index.php. The Web server knows that this is not a static resource and needs to be parsed and then returned to the browser, so the Web server launches a PHP interpreter process that implements the CGI protocol that enables communication with the back-end language PHP, and PHP returns the dynamic data back to the Web server, The Web server is then sent to the browser.
2, the drawbacks of the traditional CGI protocol

Why traditional CGI works can be a disadvantage. Because each request for a PHP script , the Web server has to open a PHP interpreter for a request (early php-cgi) process to complete the parsing. This means: Each request, the interpreter will have to initialize the php.ini and load the PHP extension, and so on, and so on, and then parsing, after the completion of the process automatically end (that is, Fork-and-execute mode).

So the CGI server has how many connection requests there will be the number of CGI child processes, the child process repeatedly loading is the main reason for the low performance of CGI. When the user requests a very large number of users will be heavily crowding out the system's resources such as memory, CPU time, etc., resulting in low performance.


2, what is fastcgi
FastCGI, a popular translation for fast CGI. FastCGI, as its name suggests, is a faster CGI that allows multiple requests to be processed within a single process, rather than ending the process directly as a request is processed, with a significant improvement in the performance of the Web server.
1. About PHP-FPM

PHP-FPM, the FASTCGI process Manager (PHP fastcgi Manager) that is specifically built for PHP. Nginx Use these PHP-FPM processes to communicate with PHP, and you can understand that PHP-FPM is interpreted as a PHP interpreter.

Unlike the traditional php-cgi interpreter, PHP-FPM implements the FASTCGI protocol and adds a lot of features. For example, PHP-FPM can smooth reboot the PHP environment configuration: We know that after modifying php.ini, Wamp and phpstudy need to reboot the server to reload the contents of the php.ini. If it is php-fpm, you do not need to restart the Web server, the original work of the PHP-FPM continue to work, such as the original work of PHP-FPM finish their work, then end their own. The newly added PHP-FPM will use the modified php.ini.


2, the realization of the FASTCGI working mode
The nginx of the FASTCGI protocol is implemented, and the first time it starts, a PHP-FPM master process is launched, which initiates multiple worker processes (that is, php-fpm in the PHP-FPM process pool), as shown in the following illustration:

When the request is sent, the master process distributes the request to the PHP-FPM in the process pool, and when it is distributed, it can take the next request, avoiding repetitive work (repeated loading of the php.ini initialization environment), and the efficiency is naturally elevated. When the request is more, Master initiates more php-fpm, and when the request is low, master stops some of the child processes. Both improves performance and saves resources. Of course, the Nginx can only return 502 errors when the PHP-FPM setting is not sufficient to support higher concurrent requests, because no more PHP-FPM processes are available. Under this protocol, PHP-FPM became the PHP interpreter for communication with PHP.
3, how nginx and PHP-FPM process communicationSo far, we know how to achieve the fastcgi php-fpm is how to work, but nginx how to start the PHP-FPM process. Nginx is not only a WEB server, but also a powerful proxy server, in addition to the HTTP request agent, can also carry out many other protocol request agent, including this article and FPM-related FASTCGI protocol. To enable Nginx to understand the FASTCGI protocol, Nginx provides a fastcgi module to map HTTP requests to corresponding FASTCGI requests.
The following is a nginx configuration file to interpret variables in Nginx as variables that PHP can understand:


Here is the configuration that nginx the PHP request to the FASTCGI module (which can be found in nginx.conf)


In this configuration file, we created a new virtual host that listens on port 80 and the Web root is/home/rf/projects/wordpress. Through the location configuration, we put all the. php file processing to the FastCGI module. After that, the FastCGI module starts the PHP-FPM (implementing the FastCGI module) and interprets the dynamic request through PHP-FPM.
Here's a flowchart I've summed up, and if there's something wrong, please correct me.


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.