How between nginx and PHP is interactive? Nginx two ways to communicate with PHP unix socket and tcp socket

Nginx + php-fpm implementation principle

Nginx本身不会对PHP进行解析,终端对PHP页面的请求将会被Nginx交给FastCGI进程监听的IP地址及端口,由php-fpm作为动态解析服务器处理,最后将处理结果再返回给nginx。其实,Nginx就是一个反向代理服务器。Nginx通过反向代理功能将动态请求转向后端php-fpm,从而实现对PHP的解析支持,这就是Nginx实现PHP动态解析的原理。

Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。

php-fpm as fastcgi process manager, memory, and can effectively control the process, and smoothing php reload configuration. After php5.3, php-fpm be integrated into the core php, the default installation, no configuration.

fastcgi process manager php-fpm own initialization, starts the main process php-fpm and start start_servers a fastcgi child process. The main process php-fpm mainly fastcgi child process management, listening 9000 port, waiting for the child process fastcgi request. When a client request arrives nginx, nginx location by instruction, all in php suffix 127.0.0.1:9000 file to be processed. php-fpm fastcgi selected and connected to a child process, and standard input and environment variables fastcgi sent to the child process. fastcgi child process to complete the process after the standard output and error messages returned. When fastcgi child process closes the connection request will come to complete the process, wait for the next deal.

Original link: https://blog.csdn.net/zhaoliang831214/article/details/51333831

A, Nginx and PHP interaction of 7-step (dynamic PHP pages users access the process)

step1: user nginx http request to the server (user and the three-way handshake nginx server TCP connection)
Step2: nginx be determined according to the request URI and user access suffix
step3: As can be seen by the second step, the user's request dynamic content, nginx fastcgi request will be handed over to the client, will be sent via fastcgi_pass user's request to php-fpm
if a user visits it is a static resource, it is simple, nginx returns directly to the static resource requested by the user to the user .
step4: after receiving the request wrapper php-fpm turn over, wrapper will create a new thread calls the php dynamic program analysis server
step5: php will query the results back to the nginx
step6: nginx construct a response message and returns the result user
this is just one of the nginx user requests and return the results of a user request is asynchronous, that is, the user requests a transfer of resources to do in the nginx, nginx can be synchronized, that is parsed resources directly to the server resources returned to the user, do not do a transit in the nginx. Step 4: fastcgi_pass dynamic resource to php-fpm, php-fpm resources will be transferred wrapper php script resolution server

Namely: Nginx -> FastCGI -> php-fpm -> FastCGI Wrapper -> php resolver

CGI is the Common Gateway Protocol, FastCGI is a CGI model program for a permanent process. We know PHP-FPM stands for PHP FastCGI Process Manager, that is PHP-FPM will be to manage the number of FastCGI process through user configuration, for example, a FastCGI process under the management of PHP-FPM hung up, based on PHP-FPM user configuration point of view if you want to reboot completion, PHP-FPM is more like a manager, while the real convergence Nginx and PHP is a FastCGI process.

FIG downstream of CGI-APP is FastCGI PHP program. And the upstream is the Nginx FastCGI, there is a communication medium between them, i.e., the figure socket. In our configuration file in Figure 3 above, the contents of the configuration of fastcgi_pass, is to tell Nginx after you receive a user request, you where to go forward in our Figure 3 is forwarded to the machine a socket file here fastcgi_pass often it configured as an http interface address (this may be arranged in php-fpm.conf). Pre-fork while in figure 5, corresponds to the start of our PHP-FPM, i.e. many FastCGI will start trigger (FastCGI the Wrapper) arranged according to a user when we start PHP-FPM

PHP provides SAPI to provide expanded Webserver oriented programming. But this approach means that if you do independent research and development of a Webserver, you will need to learn SAPI, and implement it in your Webserver program. This means that your Webserver and PHP creates coupling. Coupling solution: CGI protocol, better way is to have a common specification, it is compatible downstream. So CGI protocol became Nginx, PHP are willing to accept a way, and FastCGI mode permanent process of letting the program may have a downstream high concurrency.

Second, the two means of communication with the PHP Nginx -unix socket and tcp socket

1, both the configuration Nginx

unix socket

Need to fill pid file address php-fpm running nginx configuration file.

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
}

tcp socket

Need to fill ip address and port number php-fpm running nginx configuration file.

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
}

2, both relatively

You can see from the above picture, unix socket tcp reduce unnecessary overhead, and tcp need to go through loopback, but also apply for a temporary tcp port and related resources. However, when the unix socket when the high number of concurrent unstable connection broke out, a large amount of long-cache, there is no support in the connection-oriented protocols, large data packets may direct an error does not return an exception. Such tcp connection-oriented protocol, can guarantee the correctness and completeness of the number of communications.

3, select Recommendation: If nginx and php-fpm is running on the same server, the amount does not exceed 1000 concurrent, select the unix socket, because it is local, you can avoid some of the inspection operation (routing, etc.), and therefore faster, lighter . If faced with high concurrency business, I would choose to use a more reliable tcp socket, to load balancing, optimization, operation and maintenance of the core means to maintain efficiency.

Guess you like

Origin www.cnblogs.com/djwhome/p/12536222.html