FastCGI principle
FastCGI is an interface used for communication between HttpServer and dynamic scripting languages. Most popular Http Server supports FastCGI, including Apache, Nginx and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including PHP. (Recommended learning: nginx tutorial)
FastCGI interface adopts C/S structure, which can separate the HttP server and the script parsing server, and start one or more scripts on the script parsing server at the same time Parse daemon.
Whenever the HttP server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and then the result is returned to the client. This method allows the HttP server to exclusively process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
Nginx php-fpm implementation principle
Nginx itself will not parse PHP, and terminal requests for PHP pages will be handed over by Nginx to the FastCGI process The monitored IP address and port are processed by php-fpm as a dynamic parsing server, and the processing results are finally returned to nginx.
In fact, Nginx is a reverse proxy server. Nginx forwards dynamic requests to the backend php-fpm through the reverse proxy function, thereby realizing support for PHP parsing. This is the principle of Nginx implementing PHP dynamic parsing.
Nginx does not support direct calling or parsing of external programs. All external programs (including PHP) must be called through the FastCGI interface.
The FastCGI interface is a socket under Linux (this socket can be a file socket or an ip socket). In order to call a CGI program, a FastCGI wrapper is also needed (a wrapper can be understood as a program used to start another program). This wrapper is bound to a fixed socket, such as a port or file socket.
When Nginx sends a CGI request to this socket, through the FastCGI interface, the wrapper receives the request and then spawns a new thread. This thread calls the interpreter or external program to process the script and read the return data. ; Then, the wrapper passes the returned data to Nginx through the FastCGI interface and along the fixed socket; finally, Nginx sends the returned data to the client.
The above is the detailed content of How phpfpm and nginx communicate. For more information, please follow other related articles on the PHP Chinese website!