当前位置:网站首页>Understand CGI and fastcgi

Understand CGI and fastcgi

2022-06-26 04:06:00 Dreamers on the road

CGI

CGI(Common Gateway Interface) Public gateway interface ,CGI It's a protocol . To be exact ,CGI It's the app ( Such as PHP) And Web A standard interface for server interaction 、 agreement .

  • according to CGI standard , Write applications , It can process the data input by the client , Complete the interaction between the client and the server ;
  • CGI Defined Web How the server sends messages to the extended application , How to process the application information after receiving it ;

Let's understand the above content through a picture :

 

  • CGI yes Web The server and Web Applications (php/python etc. ) A protocol for data interaction between ;
  • FastCGI And CGI equally , It's also Web The server and Web A protocol for application interaction , It's just FastCGI In terms of efficiency CGI Better ;

CGI Implementation principles and problems

  • CGI The program runs in a separate process , And each Web Requests create a process , Destroy at the end . such “ One new process per request ” bring CGI Poor efficiency , Difficult to expand ;
  • Under high load , The operating system overhead for process creation and destruction becomes significant ;
  • Because the process address space cannot be shared ,CGI The process model limits the reuse of methods , Such as reusing database links ;

FastCGI

Fast universal Gateway Interface (Fast Common GateWay Interface) And CGI equally , It is also a way to make the application and Web Protocol of server communication .FastCGI yes CGI Enhanced version of .

  • FastCGI Less Web The server and CGI The cost of interaction between programs , This enables the server to process more web requests at the same time .

Realization

  • FastCGI Use persistent processes to process requests , These processes are made up of FastCGI Server management , instead of Web The server .
  • FastCGI Like a resident (long-live) Type CGI, It can be carried out all the time , No process creation and termination overhead per request .

FastCGI working principle

  1. Web Load when the server starts FastCGI Process Manager ( Such as PHP-FPM);
  2. FastCGI Process manager initialization itself , Start multiple CGI Interpreter process and wait for from Web Server connection ;
  3. When a client request arrives Web Server time ,FastCGI The process manager selects and connects to a CGI Interpreter .Web The server passes the environment variable and the page request through a socket or TCP connection Pass to FastCGI Subprocesses ;
  4. FastCGI After the subprocess finishes processing, standard output and error information are returned from the same connection Web Server. When FastCGI When the child process closes the connection , Request processing completed .FastCGI The subprocess then waits and processes from FastCGI Process Manager ( Running on the Web Server in ) Next connection for .

PHP-CGI

php-cgi yes php Self contained FastCGI Manager .

php-cgi Deficiency

  • php-cgi It's changing php.ini Restart after configuration php-cgi To make a new php.ini take effect , Failed to restart smoothly ;
  • Kill directly php-cgi process ,php Can't run ;

PHP-FPM

php-fpm yes PHP FastCGI Manager .FastCGI Just a protocol specification , Each language needs to implement ,PHP-FPM Namely PHP Version of FastCGI Protocol implementation .

  • Support smooth restart , After receiving the restart signal , First kill Idle worker process , new worker With the new configuration ;
  • The pressure can be increased or decreased dynamically according to the request worker Number of processes ;
     
原网站

版权声明
本文为[Dreamers on the road]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180539489486.html