Nginx and understanding the working mechanism of PHP FPM-communication? Interview inspection point (focus)

Fundamental:

Process browser to access the web

Requests for static pages

Browser(浏览器)Request http://xxx.com/aa.html-> Web Server (Nginx / Apache ) Distribution -> find the aa.htmlfile back to Browser.

Requests a dynamic script

BrowserRequest http://xxx.com/bb.php-> Web Server (Nginx / Apache ) Distribution PHP解析器-> ( PHP-CGIprogram) -> return the results to the Web Server -> return data to the Browser.

Principle: According to the server 配置文件, know that this is a PHPscript file, you need to go PHP解析器to deal with.
PHP解析器It parses the php.inifile to initialize the execution environment, and then processes the request, and then return the results to the standard data format, and finally quit the process.

CGI programs to the evolutionary history of FPM

 

CGI(Common Gateway Interface)

CGILanguage to interact with the server back-end protocol, with this protocol, developers can use any language processing server forwards the request over the dynamically generated content, to ensure that the data is passed over 标准格式the (specified 以什么样的格式传哪些数据(URL、查询字符串、POST数据、HTTP header等等)) to facilitate the developer .


PHP-CGI(PHP CGI)

PHP语言Corresponding to the interaction with the server CGI程序is PHP-CGI.

CGI程序Itself only 解析请求, and 返回结果will not 进程管理, so there is a fatal flaw, that is, each dealing with a request requires forka whole new process, along with Webthe rise of high concurrency increasingly become the norm, such inefficient way obviously can not meet demand (each time web请求there will be 启动和退出进程, that is the most criticized fork-and-executemodel, in such a large-scale concurrent, be dead).
In this way, FastCGIit was born, CGI程序and soon withdraw from the historical stage.


FastCGI(Fast CGI)

FastCGI, By definition it is faster CGI程序, to improve CGI程序performance, which allows 在一个进程内处理多个请求, rather than a request is processed directly to the end of the process has been greatly improved performance.

  • Improve performance? Then the CGI程序performance problems lie?

PHP解析器Parses the php.inifile, initializing the execution environment, it is here.
Standard CGI程序these steps for each request (not busy Leia! Start the process very tired to say!), So handle each request time will be longer. This is obviously unreasonable thing!

  • So FastCGIit is how to do it?

First of all, FastCGIwe will first start a master进程parsing configuration files, initialize the execution environment, and then start more worker进程. When a request came, masterpassed to a worker, then the next request can be accepted immediately.
This will avoid duplication of work, efficiency naturally high.
And when workerthe time is not enough, masteryou can be started in advance according to the configuration of several workermore.
Of course, idle workertime too many, also stopped a number, thus improving performance, but also saves resources. This is the FastCGImanagement of the process.

ps: there are some capable of 调度PHP-CGI进程programs, such as the lighthttpdisolated spawn-fcgi. Well, PHP-FPMis such a stuff, after a long period of development, has been gradually recognized by everyone (but you should know a few years ago to complain about PHP-FPMpoor stability), are increasingly popular.


 

PHP-FPM(FastCGI Process Manager)

It is FastCGI协议an implementation of any implementation of the FastCGI协议servers are able to communicate.
FPMThe standards-based FastCGI程序, also provides some enhanced features, specifically refer to the official documentation: PHP: FPM Installation .

  • FPMIt is a PHP进程管理器containing masterand workertwo kinds of processes.

master进程Only one, responsible for monitoring port, receiving a request from the server, and worker进程then generally have multiple (based on the actual need to configure specific number), are embedded within each process a PHP解释器, it is PHP代码where the real execution, the following is my native FPMof process conditions: 1 master进程, 2 worker进程.

$ ps -ef | grep fpm
root       130     1  0 01:37 ?        00:00:01 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
php-fpm    131   130  0 01:37 ?        00:00:00 php-fpm: pool www
php-fpm    133   130  0 01:43 ?        00:00:00 php-fpm: pool www
  • From FPMreception of the request, the processing is completed, the specific process is as follows:
  1. FPMThe master进程receipt of the request.
  2. master进程Depending on the configuration assigned a specific worker进程request processing, if the process is not available, an error is returned, which is why we cooperate Nginxencounter 502more of the wrong reasons.
  3. workerProcess to process the request, if time-out, return 504error.
  4. Request processing ends and returns the result.
  • FPMFrom receiving the flow of processing requests is the case, then Nginxit is how to send requests to FPMit?

This requires from Nginxstraightforward level,.

We know, Nginxit not just a Web服务器, is a powerful feature Proxy服务器, in addition to http请求the agent, the agent can also be many other protocol requests, including paper and FPMrelevant FastCGI协议. In order to make the Nginxunderstanding FastCGI协议, Nginxit is provided FastCGI模块to be http请求mapped to a corresponding FastCGIrequest.

NginxIs FastCGI模块provided fastcgi_param指令to the main processing 映射关系, the following is Nginxan example of a configuration file, which is the main work Nginxtranslated variables to PHPvariables can be appreciated.

$ cat /usr/local/nginx/conf/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

In addition, the very important thing is fastcgi_pass指令, this command is used to specify FPM进程the address of the listener, Nginxwill all PHP请求translated FastCGI请求and then sent to this address later. Here's a simple can work Nginx配置文件:

server {
    listen 80;
    server_name test.me;
    root /usr/local/web/myproject/public;
    index index.php index.html index.htm;

    access_log /usr/local/nginx/logs/test-access.log;
    error_log  /usr/local/nginx/logs/test-error.log;

    location / {
      try_files $uri $uri/ /index.php?$query_string;
    }

    location ~\.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/local/web/myproject/public/$fastcgi_script_name;
        fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;
        fastcgi_index index.php;
    }
}

In this profile, we created a new 虚拟主机, listening 80端口, 项目根目录as /usr/local/web/myproject/public. By then we location指令will all to .php结尾the requests to FastCGI模块process, thus bringing all PHP请求are handed over to FPMtreatment, thus completing Nginxthe FPMclosed loop.
So since Nginxthe FPMentire process of communication should be more clear.

 
image
  • Modify php.inithe configuration file, use PHP-FPMWhy can smooth restart?

Modification php.iniafter, PHP-CGI进程there is no way a smooth restart.
PHP-FPMThe mechanism of this process is new worker进程with the new configuration, existing worker进程processed can recover from the hands of the living, and through this mechanism 平滑过渡.

Guess you like

Origin www.cnblogs.com/beyond-succeed/p/12615059.html