The difference between CGI, mod_php and FastCGI

CGI now basically no one use, unsafe and performance is extremely low, the increasing use of web built-in expansion, fastCGI. Such as Microsoft iis the ISAPI, apache the php module, Nginx the php-cgi. CGI, built-in module, fastcgi these three properties belong to the best fast_cgi fastest, but requires additional process. CGI and FASTCGI resolution to look any different.
CGI way of introduction:
cgi used more than in 2000 or earlier, before the web server generally only handle static requests, if you encounter a dynamic requests how to do it? web server based on the content of the request, then will fork a new process to run an external program c (or perl script ...), this process will processed data back to the web server, the web server sends the contents of the last to users just fork process also will withdraw. If the next time the user requests also change the dynamic scripts, web server and a fork a new process again, again and again carried out.

built-in module describes the web:
the subsequent emergence of a more advanced way, web server can be built perl interpreter or php interpreter. That made way these interpreter module, web server started these interpreters at startup. When a request comes a new dynamic, web server is to resolve these themselves perl or php script, to save re-fork a process, efficiency is improved.

fastcgi way of introduction:
fastcgi way is when the web server receives a request, he would not re-fork a process (since this process started when the web server is turned on, and it will not withdraw), web server to deliver content directly to the this process (inter-process communication, but fastcgi used in other ways, tcp way communication), the process for processing after receiving the request, the results back to the web server, and finally his own request and then waits for the next, rather than quit.

cgi fastcgi with the difference between table:

name In the web server side In terms of process data processing
cgi Fork a new process for processing Read parameters, process the data, and then end of life
fastcgi Connecting with tcp way with the process on a remote machine or a local process To open the tcp port into the loop, waiting for the arrival of data, data processing

For example: the server now has a 100,000 word word, every customer will be sent a string, the string is asked to prefix the word how many. You can write a program that will build a trie tree, and then every time the user requests directly to the trie to find the time to come. But if the cgi way, then this request after we finish this trie has gone, and so the next time you start the process, but also a new trie tree, so the efficiency is too low down. And then this lesson the trie established with fastcgi way when the process starts, since you can directly query the specified prefix in trie tree.

CGI works: Every time a customer requests CGI time, WEB server requesting the operating system to generate a new CGI interpreter process (such as php-cgi.exe), when the CGI to meet the requirements, the WEB server to kill the process.
    So, CGI interpreter repeated loading is the main reason for poor performance CGI , if CGI interpreter kept in memory and accept scheduling FastCGI process manager, can provide good performance, scalability, Fail- Over characteristics and so on.

    FastCGI's official site http://www.fastcgi.com works FastCGI is:  1, loaded FastCGI Process Manager Web Server start when the [PHP FastCGI Process Manager is FPM-PHP (PHP -FastCGI Process Manager) ] .   2, FastCGI process manager initializes itself, to start multiple CGI interpreter process (php-cgi.exe more visible in the Task Manager) and waits for a connection from the Web Server. When you start php-cgi FastCGI process, you can configure the TCP and UNIX sockets (socket) are two ways to start.   3, when a client requests Server reaches the Web, the Web Server protocol request using TCP socket or forwarded to the embodiment FastCG the I main process, FastCG the I selected and connected to the main process a CGI interpreter (subprocess php-cgi.exe

  


). Web server sends to the child FastCGI php-cgi.exe CGI environment variables and the standard input.
   4, FastCGI php-cgi.ex child process after completion of treatment standard output and error messages from the same connection returns Web Server. When the child FastCGI process closes the connection request will come to complete the process. FastCGI child process then waits for and FastCGI process manager process from the lower (running in the WebServer) is connected. In the CGI, php-cgi child in this process it will be pulled out.

  In these cases, you can imagine how CGI is usually slow. Each Web request must be re-parse PHP php.ini, reload all dll extension and try to initialize the entire data structure. Use FastCGI, all of which occur only once when the process started. An additional benefit is that persistent database connections can work.
PHP can make a program from the original request - start - run to the request - mode of operation, greatly improving the efficiency of the script, to reduce the load on the server.

Two: the advantages of PHP FastCGI:
1.php script run faster, php interpreter is loaded into memory rather than reading from memory each time when needed, greatly enhance the performance of the site rely on scripts to run.
2. The need to use fewer system resources, because the server every time you need not have php interpreter loaded, you can enhance the transmission speed of the site without having to add a lot of cpu load.

Guess you like

Origin blog.csdn.net/mybluesky1983/article/details/51711242