The promotion strategy of the main process (6): CGI and FastCGI

The message is transmitted through the network and reaches the server side. The most common server is the Web server. Students who do PHP know that PHP in FastCGI mode is more efficient than ordinary PHP. This article will talk about the principle.

 

Old but common CGI

The web server can parse HTTP requests and return static resources (HTML pages, pictures, etc.), but to output dynamic content, external programs such as PHP/C#/Ruby/Java/Python/C/C++ must be implemented.

 

In the early days, there was a technology called CGI (Common Gateway Interface), which was a standard for transferring data between Web servers and external programs. A simple CGI program (C++ language) is as follows:

#include <stdio.h>
#include <stdlib.h>
intmain()
{
       printf("Content-type: text/html\r\n\r\n");
       printf("your name is:%s\n",  getenv("QUERY_STRING"));
       return 0;
}

 When the browser accesses this CGI program, it will display: your name is:name=xxx

 

CGI specifies how to transfer data between the Web server and CGI programs. The specific process is roughly as follows:

1. After the web server receives the request information, start the CGI program (apache is the fork process exec CGI program);

2. The Web server passes the request information to the CGI program through environment variables and standard input ;

3. After the CGI program executes the business logic, the response data is returned to the Web server through standard output and standard error , and the CGI program exits ;

4. The web server is then organized into HTTP response packets and sent to the browser.

 

In the above example, the first line of printf is to output the HTTP header (remember that HTTP Header and Body are separated by \r\n\r\n?), getenv("QUERY_STRING") is to get the URL from the environment variable, printf returns content via standard output.

 

What information does the web server pass to the CGI program through environment variables? These are commonly used:

  • CONTENT_LENGTH : Number of bytes of data sent to standard input (POST)
  • QUERY_STRING: actually stores the data sent to the CGI program (GET)
  • REQUEST_METHOD: The CGI method (GET or POST) used to transfer the data
  • HTTP_COOKIE:cookie值
  • REMOTE_ADDR: User IP
  • SCRIPT_NAME: Requested CGI

It can be seen that CGI is just a standard, and CGI programs can be written in any language, as long as the language has standard input, standard output and environment variables, such as: C/C++, perl, PHP, ruby. According to the requirements of the CGI standard, it can interact with the Web server.

 

FastCGI came into being

CGI transmits data through environment variables/standard input, standard output/standard error, and its running performance is relatively low. There are two main points:

1. Each request requires the web server to fork out the CGI program, and frequent fork processes are time-consuming

2. The CGI program runs from scratch every time. It takes time to read the configuration and connect to other services.

 

FastCGI is an improvement to CGI. In FastCGI mode, the process of data transmission between the Web server and the FastCGI program is roughly as follows:

1. After the web server receives the request information, it sends the request information to the FastCGI program through the socket according to the FastCGI protocol;

2. After the FastCGI program executes the business logic, it returns the response data to the Web server through the socket , and the FastCGI program does not exit ;

3. The web server is then organized into HTTP response packets and sent to the browser.



 

Comparing the passing of CGI, it can be found that the process of each fork is mainly omitted, and the socket is used to transmit data, which is the reason why the FastCGI interface is more efficient.

 

FastCGI has these features:

  • FastCGI programs are resident in memory and can process requests repeatedly after startup
  • FastCGI is a general synchronization server framework for process pool/thread pool model

The FastCGI program will not exit after processing the request, and it can process the request repeatedly. Then, after startup, the configuration parsing and connection with other backgrounds are established, and there is no need to do it every time the request is made, which is naturally faster.

 

As for how to implement the city pool/thread pool inside this FastCGI, it is the matter of the FastCGI process manager (FastCGI engine). C/C++ FastCGI commonly used mod_fastcgi module of apache, PHP commonly used spawn-fcgi and PHP-FPM.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326452810&siteId=291194637