Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Nginx and PHP-FPM communicate with each other.

  • You can learn the ins and outs of communication between nginx and PHP-FPM;

  • You have some understanding of the relationships between CGI, FastCGI, PHP parsers, and so on.

  • You can get a general idea of how nginx communicates with PHP-FPM;

Noun explanation

Web Application

It refers to applications developed in programming languages such as PHP and Java, including websites and mobile applications.

Server (Web Server)

Generally refers to Nginx, Apache, Lightpd, Tomcat, etc.

Web servers are simply distributors of content. For example, if the client requests /index.html, Nginx will go to the file system to find the file and send it to the browser, which distributes static data. If the client now requests /index.php, Nginx knows from the configuration file that this is not a static file and needs to go to the PHP parser to process it, then it will simply pass the request to the PHP parser.

CGI (Common Gateway Interface)

Common Gateway Interface (CGI) : a Common Gateway Interface.

It is not a programming language, but a protocol, a specification for running external programs on a Web server.

This external program is called CGI program, that is, the implementation of the CGI interface standard program, as long as the programming language has standard input, standard output, can be used to write CGI programs.

The code files of each dynamic language (PHP, Python, etc.) need to be parsed by the corresponding parser before being recognized by the server. The CGI protocol is used to make the parser and the server can communicate with each other. The file parsing on the server needs to use the PHP parser, plus the corresponding CGI protocol, so that the server can parse PHP files.

In plain English, web servers need to interact with external programs, so they need to know what the two parties want. For example, what data does Nginx transmit to the PHP parser? Urls, query strings, POST data, HTTP headers, CGI is a protocol that specifies what data to pass and in what format to the backend to process the request.

disadvantages

CGI program makes it possible for external programs to interact with web server, but it has great problems because of CGI running mode.

CGI mode works like this: when Nginx receives the browser /index.php request, it first creates a corresponding CGI process, in this case php-cgi (PHP parser). Php-cgi then parses the php.ini file, initializes the execution environment, processes the request, returns the result in a CGI format, and exits the process. Finally, Nginx returns the results to the browser, and then destroys the process. The whole process is a fork-and-execute pattern.

So php-CGI receives the data from CGI and executes the following process: initialize the environment -> process the request -> return the result -> kill the process. Each request will correspond to a PHP-CGI process.

As you can see, if there are thousands of requests, then thousands of repeated create and destroy process operations will be repeated. This approach is easy to implement, but very inefficient. And because the process is independent, the address space cannot be shared and the reuse of resources is limited. For example, the php.ini file is parsed every time the process is created, which is not necessary when the php.ini file has not changed.

Based on these problems, FastCGI protocol was introduced later, which is also an improvement and optimization of CGI protocol.

advantages

The ADVANTAGE of the CGI mode is that it is completely independent of any server and serves only as an intermediary: it provides an interface to the Web server and the scripting language, which use the CGI protocol to wire up data transfer. The advantage of this is to minimize the relationship between them and make them more independent of each other.

FastCGI (Fast Common Gateway Interface)

FastCGI is the Fast Common Gateway Interface, which can be understood as an improved version of the CGI protocol.

FastCGI aims to reduce the overhead of interaction between Web servers and CGI programs so that servers can handle more Web requests at the same time.

So how does FastCGI do this?

FastCGI is designed with one process manager (Master process) and multiple Worker processes (Worker process).

The FastCGI mode works as follows:

  1. Start the FastCGI process manager;

  2. Parse the php.ini file and initialize the execution environment.

  3. Start multiple CGI protocol interpreter daemons and wait for connections from the Web server;

  4. When a client request arrives at the Web server, the FastCGI process manager selects and connects to a CGI interpreter, and the Web server sends CGI environment variables and standard input to the FastCGI child process php-CGI, The php-CGI child processes the standard data and error messages back to the Web server; The phP-CGI child closes the connection, the request is processed, and the next requested connection from the FastCGI process manager is waited for and processed.

The execution process of FastCGI is as follows: start the FastCGI process manager -> initialize the execution environment -> start multiple Worker processes -> Master process receives the request, allocates the request to the Worker process, and the Worker process processes the request.

advantages

  1. Good performance, by setting the management process and work process, to solve the PROBLEM of frequent CGI create process, modify process. It is more of a resident CGI program than CGI, that is, it keeps the CGI interpreter process running in memory at all times. It can run all the time without having to create a new process to process and then destroy every Web request, thus achieving higher performance.

  2. High stability, FastCGI mode is an independent process pool to run CGI protocol program, a single process dies, the system can easily discard, and then reassign a new process to run logic.

PHP-CGI

Php-cgi is the interface program of CGI protocol provided by PHP (Web Application) to web server. Php-cgi was an early FastCGI manager for PHP implementations, and although it was officially shipped at the time, it was poorly performing.

Prior to PHP 5.3, FastCGI Web requests were implemented using PHP-CGI. As of PHP 5.4, php-FPM has replaced php-CGI, which is responsible for process management.

use

PHP - cgi - b 127.0.0.1:9000Copy the code

disadvantages

  • Every time a PHp-CGI program processes a request, it has to load the php.ini file, load all the extension modules, and do a lot of repetitive work.

  • Every time the php.ini file is changed, the php-cgi program needs to be restarted for the new php.ini file to take effect.

  • Just kill the php-cgi program and PHP will not run (php-fpm does not have this problem, the daemon will smoothly start the new child process)

Php-cgi is a CGI program that itself only handles parsing requests and returns results. It does not manage the process. So then there were programs that could schedule php-CGI processes, and that’s where php-fPM came in.

PHP-FPM

Php-fpm stands for PHP FastCGI Process Manager, the FastCGI Process Manager.

Php-fpm is a master/ worker-based multi-process architecture similar to nginx’s design style. The master process is responsible for CGI, PHP environment initialization, event listening, child process state, while the worker process is responsible for PHP requests.

The master of FPM obtains the information of worker process through the shared memory, including the current status of worker process, number of processed requests, etc. When the master process wants to kill a worker process, it will notify the worker process by sending a signal.

The implementation of FPM begins by creating a master process, creating and listening on sockets in the master process, and then forking out child processes that each accept requests. The child process is simple: it blocks on Accept when it starts, reads request data when it arrives, processes and returns, and receives no other requests during that time. It is different from nginx’s event-driven model. Nginx is a non-blocking model in which the next request is processed if the data is not sent, meaning that a process can connect to multiple requests at the same time.

advantages

  • Provides a better way to manage PHP processes, support smooth stop/start processes;

Nginx interacts with PHP-FPM

Take the common LNMP architecture, where Nginx is on one server and PHP is on another.

When we request a domain name, such as a test domain name, www.test.com, the domain name is resolved to the Nginx server and Nginx routes to the index.php file. Nginx detects that this is not a static file and needs to find a PHP parser to parse it, then load the Nginx FAST_CGI module and fastcgi_pass it to the PHP server, such as 10.20.0.1:9000. The phP-FPM child on the PHP server listens for the request and processes it. The process is as follows:

Browser to access www.test.com | | DNS to Nginx server | | routed to www.test.com/index.php | | Nginx detection index. PHP is not a static resource, Loading Nginx fast - cgi module | | request has been forwarded to the PHP on the server to the location of the | | fast - cgi PHP server monitoring 127.0.0.1:9000 address | | www.test.com/index.php Request to 127.0.0.1:9000 | | PHP - FPM worker process execution codeCopy the code

Nginx communicates with PHP-FPM

We’ve already seen how Nginx interacts with PHP-FPM. Let’s take a look at how Nginx and PHP-FPM communicate.

On Linux, Nginx and PHP-FPM communicate in two ways, TCP-socket and UNIX-socket.

When Nginx and PHP-FPM are not on the same machine, only tcp-socket communication can be used.

The process of the two modes of communication is shown below (to borrow a picture from the Internet) :

Unix domain socket

In Linux, there are many ways to communicate between processes, and sockets are one of them. However, traditional sockets are based on the TCP/IP protocol stack and need to specify the IP address. Of course this is fine if two processes on different hosts communicate. However, if you only need to communicate between two different processes on the same machine, using IP addresses is a bit of overkill.

For sockets, there is also a category called Unix domain sockets. Designed to solve this problem.

Unix Domain sockets, also called INTER-process Communication (IPC) sockets for short, are developed based on sockets and used to implement inter-process communication on the same host. Socket was originally used for communication between processes on different machines, and of course, it can also be used for communication between processes on the same machine (via localhost). Later, based on this, AN IPC mechanism was developed for communication between processes on the same machine. Compared with the original network socket, No need to go through the network protocol stack, no need to pack and unpack, calculate checksums, maintain serial numbers and replies, etc., just copy application layer data from one process to another. This is because IPC mechanisms are essentially for reliable communication, while network protocols are designed for unreliable communication.

Comparison between TCP socket and Unix socket

  • Efficiency: In theory, Unix Domain sockets do not need to go through the network protocol stack, do not need to pack and unpack, calculate checksums, maintain serial numbers and replies, etc., just copy application layer data from one process to another. Therefore, the efficiency of TCP socket is higher than that of TCP socket, reducing unnecessary TCP overhead.

  • Cross-machine communication: TCP sockets support cross-machine communication, whereas Unix Domain sockets communicate between the same machine processes.

  • High concurrency: In fact, in high concurrency, the performance difference between the two is not significant. But TCP sockets can show significantly higher stability.

Nginx configuration

127.0.0.1:9000 fastcgi_pass 127.0.0.1:9000 fastcgi_pass 127.0.0.1:9000 /dev/ SHM is a TMPFS (temporary file system that resides in memory). Sock is much faster than disk # fasrcgi_pass /dev/shm/ php-ftp.sockCopy the code

PHP – FPM configuration

Sock = /dev/ SHM /php-fpmCopy the code

Note: In Unix socket communication, because the socket file is essentially a file, there is a problem of permission control, so you need to pay attention to the Nginx process permission and phP-fpm permission, otherwise there will be a message indicating no permission to access. (Set users in their own profiles)

conclusion

  1. What is CGI?
  • CGI is a protocol that defines the specification for the Web server to execute external programs, including the format and content of data passed between the Web server and external programs.
  1. The difference between FASTCGI and CGI?
  • FASTCGI is an improved version of CGI, the CGI fork-and-execute mode, under high concurrency, frequently create, destroy process, very inefficient, FASTCGI by setting the management process And worker process, solve the problem of CGI frequently create process, modify process.
  1. What are the communication mechanisms between PHP-FPM and Nginx?
  • TCP socket and Unix socket
  1. What is the difference between a TCP socket and a Unix socket?
  • Efficiency: Theoretically, since Unix sockets are interprocess communication, they should be more efficient.

  • Machine-to-machine communication: TCP sockets support machine-to-machine communication, whereas Unix sockets do not.

  • High concurrency: In fact, tests show that TCP socket communication is more stable under high concurrency.

  1. Which method is recommended?
  • TCP sockets are recommended for the following reasons:
  1. Support cross-machine communication;

  2. High concurrency services are more reliable;

  3. If Nginx wants to do load balancing, it doesn’t need to consider Unix socket mode, only TCP socket mode.

Reference documentation

  • NGINX and PHP-FPM communication in detail

  • Nginx communicates with PHP-FPM

  • PHP-FPM and Nginx communication mechanism summary

  • PHP runtime mode

  • Nginx communicates with PHP-FPM

  • Php-fpm and Nginx communication mechanism