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

Source: Internet
Author: User
There are many articles on how to configure Nginx + FPM on the network, but they are more from the operational point of view, tell us how to do, but did not tell us why to do so, this article from Nginx and FPM working mechanism, discuss the principle behind the configuration, let us really understand Nginx and PHP is How to work together. To say how Nginx and PHP work together, the first thing to say is the CGI (Common Gateway Interface) and FastCGI the two protocols.

CGI is the protocol for Web server to interact with the background language, and with this protocol, developers can use any language to process requests from the Web server and dynamically generate content. But CGI has a fatal disadvantage, that is, every request to deal with a new process fork, with the rise of the Web, high concurrency is becoming the norm, so inefficient way obviously can not meet the demand. In this way, FastCGI was born, and CGI soon withdrew from the stage of history. FastCGI, as the name implies for faster CGI, which allows processing of multiple requests within a process, rather than simply ending the process with a request being processed, has a significant performance improvement.

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 to the standard FastCGI also offers some enhancements, specifically for the official documentation: PHP:FPM installation.

FPM is a PHP process manager that contains both master and worker processes: The master process has only one, is responsible for listening to the port, receives requests from the Web Server, and the worker process is typically multiple (the exact number is configured according to actual needs), each Inside the process is embedded a PHP interpreter, is the PHP code to really execute the place, is my on-board FPM Process situation, 11 master processes, 3 worker processes:

The request is received from the FPM, and the process is completed with the following specific procedures:

    1. FPM's master process receives the request

    2. The master process assigns a specific worker process based on the configuration for request processing, and if there are no processes available, returns an error, which is why we have encountered more than 502 errors with Nginx.

    3. The worker process processes the request and returns a 504 error if it times out

    4. Request processing end, return result

The flow of fpm from receiving to processing requests is that, 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 the proxy for HTTP requests, can also make a number of other protocol request agent, including this article with the FPM-related fastcgi protocol. To enable Nginx to understand the FASTCGI protocol, Nginx provides a fastcgi module to map the HTTP request to the corresponding fastcgi request.

Nginx fastcgi module provides the fastcgi_param instruction to deal with these mappings mainly, the following Ubuntu under the Nginx configuration file, the main task is to translate the Nginx variables into PHP can understand the variables.

In addition, it is very important that the fastcgi_pass instruction is used to specify the address that the FPM process listens to, and Nginx will translate all PHP requests into the FASTCGI request before sending it to this address. The following is a simple Nginx configuration file that can work:

In this configuration file, we create a new virtual host, listening on port 80, the Web root directory is/home/rf/projects/wordpress. We then pass all the requests that end with. PHP to the FastCGI module through the location command, thus handing all PHP requests to FPM processing, thus completing the Nginx-to-fpm closed loop.

So, the whole process of Nginx and FPM communication should be clearer.

Above

April 2, 2016 Beijing

[note] This article starts with the http://zhuanlan.zhihu.com/p/20694204 column

  • 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.