The operating principle of php, cgi vs fastcgi, and the difference between php-cgi and php-fpm

Recently, the local test environment of the project encountered the blocking problem caused by nginx using file_get_contents/curl to access php files in the windows environment. I have been looking for a solution. After three days of research, I finally found a solution, especially because of this, I am I am interested in the operating principle of php, so I will record it to a certain extent here. There may be mistakes and omissions, and corrections are welcome.

 

To understand how php works, you must first understand the following concepts:

 

CGI:

 

The English of CGI is the (COMMON GATEWAY INTERFACE) public gateway interface. Its function is to help the server communicate with the language. Here is the communication between nginx and php. Because the languages ​​of nginx and php are different, a process of communication and conversion is required, and CGI is the This communication protocol.

 

After the nginx server receives the data passed by the browser, if the request is a static page or image that does not need dynamic processing, it will directly find its location according to the requested url and return it to the browser. There is no need for PHP to participate here, but if It is a dynamic page request. At this time, nginx must communicate with php. At this time, the cgi protocol will be used to convert the request data into information that php can understand, and then the information returned by php based on this information must also pass the cgi protocol. Convert it into information that nginx can understand, and finally nginx receives this information and returns it to the browser.

 

fast-cgi:

 

The traditional cgi protocol will open a process for processing each time a connection request is made, and the process will be closed after processing. Therefore, the next connection will need to open a process again for processing, so there are as many cgi processes as there are connections. , which is why traditional cgi can appear slow, so too many processes consume resources and memory.

 

Fast-cgi is a process that can handle multiple requests, which is completely different from the above cgi protocol. cgi is a process that can only handle one request, which will lead to a large number of cgi programs, which will put a burden on the server.

 

php-cgi:

 

php-cgi is the cgi protocol interface program provided by php to web serve, that is, the http front-end server. When receiving a request from the http front-end server, a php-cgi process will be opened for processing, and the process of opening php-cgi will be First, you need to reload the configuration, data structure and initialize the running environment. If you update the php configuration, you need to restart php-cgi to take effect, such as phpstudy.

 

php-fpm:

 

php-fpm is the fastcgi protocol interface program provided by php to web serve, which is the http front-end server. It does not restart a process every time it connects like php-cgi, and then closes the process after processing the request, but allows a process Handles multiple connections without closing the process immediately, but will continue to handle the next connection. It can be said to be a management program of php-cgi and an improvement to php-cgi.

 

php-fpm will open multiple php-cgi programs, and php-fpm resides in memory. Every time the web serve server sends a connection, php-fpm assigns the connection information to one of the following subprograms, php-cgi, for processing After processing, the php-cgi will not be closed, but will continue to wait for the next connection, which is also the principle of fast-cgi acceleration, but because php-fpm is multi-process, and a php-cgi basically consumes 7-25M memory , so if there are too many connections, it will cause excessive memory consumption and cause some problems, such as 502 errors in nginx.

 

At the same time, php-fpm also comes with some other functions:

 

For example, for smooth transition configuration changes, ordinary php-cgi needs to be restarted to initialize the new configuration after each configuration change, while php-fpm does not need it, php-fpm sends new connections to the new subprogram php -cgi, the new configuration is loaded at this time, and the original running php-cgi still uses the original configuration. After this connection, the next connection will be initialized with the new configuration, which is a smooth transition.

 

It may be difficult to understand the above text description. The following graphics briefly illustrate the process of browser requesting web server, cgi and fastcgi, as well as the difference and connection between php-cgi and php-fpm:

The above is the process of using the dynamic page of php-fpm, and the following supplements the case where there is no ordinary cgi protocol;

The web server here can be nginx, or an http server such as IIS and apache, or a website server or front-end server.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325808910&siteId=291194637