Home >Backend Development >PHP Tutorial >Sharing examples of the working mechanism of Nginx and FPM
There are many articles on the Internet about how to configure Nginx + FPM, but they are more from an operational perspective and tell us how to do it, but they do not tell us why we should do it. This article starts from the perspective of Nginx and FPM Starting from the working mechanism, we discuss the principles behind the configuration, so that we can truly understand how Nginx and PHP work together.
To talk about how Nginx and PHP work together, we must first talk about the two protocols CGI (Common Gateway Interface) and FastCGI.
CGI is a protocol for interaction between Web Server and background language. With this protocol, developers can use any language to process requests from Web Server and dynamically generate content. But CGI has a fatal shortcoming, that is, each request needs to be forked into a new process. With the rise of the Web, high concurrency has become more and more the norm, and this inefficient method obviously cannot meet the demand. In this way, FastCGI was born, and CGI soon withdrew from the stage of history. FastCGI, as the name suggests, is faster CGI. It allows multiple requests to be processed in one process, instead of ending the process directly after one request is processed. The performance has been greatly improved.
As for FPM (FastCGI Process Manager), it is an implementation of FastCGI, and any Web Server that implements the FastCGI protocol can communicate with it. FPM also provides some enhancements to standard FastCGI. For details, please refer to the official documentation: PHP: FPM Installation.
FPM is a PHP process manager, including the master process and the worker process: there is only one master process, which is responsible for listening to the port and receiving requests from the Web Server, while there are generally multiple worker processes (specifically The number is configured according to actual needs). Each process has a PHP interpreter embedded inside it, which is where the PHP code is actually executed. The picture below is the process situation of fpm on my local machine, 1 master process and 3 worker processes:
After receiving the request from FPM and completing the processing, the specific process is as follows:
of FPM The master process receives the request
The master process assigns a specific worker process according to the configuration to handle the request. If there is no available process, an error is returned. This is why we encounter more 502 errors when working with Nginx. reason.
The worker process processes the request. If it times out, a 504 error is returned.
The request processing is completed and the result is returned
The process of FPM from receiving and processing the request is like this, so how does Nginx send the request to FPM? This needs to be explained from the Nginx level.
We know that Nginx is not only a Web server, but also a powerful Proxy server. In addition to proxying http requests, it can also proxy many other protocol requests, including the fastcgi protocol related to fpm in this article. . In order to enable Nginx to understand the fastcgi protocol, Nginx provides the fastcgi module to map http requests to corresponding fastcgi requests.
Nginx's fastcgi module provides the fastcgi_param instruction to mainly handle these mapping relationships. Below is a configuration file for Nginx under Ubuntu. Its main task is to translate the variables in Nginx into variables that can be understood in PHP.
In addition, a very important thing is the fastcgi_pass directive. This directive is used to specify the address where the fpm process listens. Nginx will translate all php requests into fastcgi requests and then send them to this address. Here is a simple working Nginx configuration file:
In this configuration file, we create a new virtual host, listening on port 80, and the Web root directory is /home/rf/projects/wordpress. Then we use the location directive to hand over all requests ending with .php to the fastcgi module for processing, thus handing over all PHP requests to fpm for processing, thus completing the closed loop from Nginx to fpm.
Now, the entire process of communication between Nginx and FPM should be relatively clear.
Related recommendations:
PHP -Detailed explanation of real-time viewing and monitoring of FPM running status
phpUse php-fpm to restart and stop the operation command
The above is the detailed content of Sharing examples of the working mechanism of Nginx and FPM. For more information, please follow other related articles on the PHP Chinese website!