In-depth understanding of PHP: working mechanism of Nginx and FPM

Source: Internet
Author: User
In-depth understanding of PHP: Nginx and FPM work mechanisms many articles on how to configure Nginx + FPM on the network, but they tell us how to do it from the operational perspective, however, this article does not tell us why we should do this. starting from the working mechanism of Nginx and FPM, This article explores the principles behind configuration, so that we can truly understand how Nginx and PHP work together. To learn how Nginx and PHP work together, we must first talk about the CGI (Common Gateway Interface) and FastCGI protocols.

CGI is a protocol for interaction between the Web Server and the background language. with this protocol, developers can use any language to process requests sent by the Web Server and dynamically generate content. However, CGI has a fatal drawback: every time a request is processed, it needs to fork a new process. with the rise of Web, high concurrency has become the norm, this inefficient method obviously cannot meet the needs. In this way, FastCGI was born, and CGI quickly exited the stage of history. FastCGI, as its name implies, is a faster CGI. it allows multiple requests to be processed in a process, rather than directly ending the process after a request is processed, which greatly improves the performance.

As for FPM (FastCGI Process Manager), it is the implementation of FastCGI, and any Web Server that implements the FastCGI protocol can communicate with it. FPM also provides some enhancements to the standard FastCGI. For more information, see the official documentation: PHP: FPM Installation.

FPM is a PHP process manager that contains two processes: master process and worker process. The master process has only one process, which listens to the port and receives requests from the Web Server, there are generally multiple worker processes (the specific quantity is configured according to actual needs). each process is embedded with a PHP interpreter, which is the place where PHP code is actually executed, it is the fpm process on my local machine. one master process and three worker processes:

The process from FPM receiving to processing is as follows:

  1. The FPM master process receives the request.

  2. The master process assigns a specific worker process according to the configuration for request processing. If no process is available, an error is returned. this is also the reason why we encounter many 502 errors with Nginx.

  3. The worker process processes the request. if the request times out, Error 504 is returned.

  4. The request processing is complete and the returned result is

The process for FPM from receiving to processing requests is like this. how does Nginx send requests to fpm? This needs to be clarified from the Nginx layer.

We know that Nginx is not only a Web server, but also a powerful Proxy server. in addition to http request Proxy, Nginx can also be used as a Proxy for many other protocol requests, this includes the fastcgi protocol related to fpm. To enable Nginx to understand the fastcgi protocol, Nginx provides the fastcgi module to map http requests to the corresponding fastcgi requests.

The fastcgi module of Nginx provides the fastcgi_param command to mainly process these mappings. The following is a configuration file of Nginx in Ubuntu, the main task is to translate the variables in Nginx into variables that can be understood in PHP.

In addition, the fastcgi_pass command is very important. This command is used to specify the address where the fpm process listens. Nginx translates all php requests into fastcgi requests and then sends them to this address. The following is a simple Nginx configuration file that can work:

In this configuration file, a new virtual host is created, listening to port 80, and the Web root directory is/home/rf/projects/wordpress. Then we run the location command. all requests ending with php are sent to the fastcgi module for processing, so that all php requests are handed over to fpm for processing, thus completing the closed loop from Nginx to fpm.

So far, the entire process of communication between Nginx and FPM should be clear.

Above

April 2, 2016 Beijing

[Note] this article first and Zhihu column http://zhuanlan.zhihu.com/p/20694204

Related Article

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.