FastCGI

from Wikipedia, the free encyclopedia

FastCGI is a binary network protocol for connecting an application server to a web server . FastCGI is comparable to the Common Gateway Interface (CGI), but was developed to circumvent its performance problems.

Difference to the CGI

When a CGI-based website is called up, the web server starts a process of the CGI program and ends it again at the end of the request. Because CGI programs are often written in a scripting language such as Perl , this means that the often quite extensive interpreter has to be loaded for each page view , which means a lot of overhead (loading the interpreter with simple CGI programs takes longer than the actual program execution) . In addition, each request needs its own interpreter, i. In other words, if there are several parallel requests, there are correspondingly several copies of the interpreter in the server's main memory. CGI has become very widespread due to its simplicity, independence from the programming language and extensive support from practically all web servers, but the above-mentioned overhead leads to high latency and, with heavily used servers, quickly to overload.

In contrast to this, with FastCGI the program to be executed (including interpreter, if necessary) is only loaded once and is then available for multiple requests - regardless of whether from the same client or from different clients. Communication with the web server does not take place via environment variables and standard input / output , but via Unix domain sockets or TCP network connections, i.e. In other words, the program can even run on another computer.

In terms of programming, the difference to CGI programs can be determined by the fact that a FastCGI program has a central loop that accepts requests and can run for as long as the web server:

 use FCGI;
 $var = 'foo';
 while (FCGI::accept () >= 0) {
   ... http-Request bearbeiten ...
 }

While running through this loop, variables are retained in the memory, which on the one hand enables further optimization possibilities compared to CGI programs, and on the other hand requires more careful programming in order to avoid memory leaks.

functionality

Communication with the web server is packet-oriented and connectionless . A data packet contains the FastCGI protocol version, the message type, a request ID and the length of the following data in the header . The message type largely corresponds to the data sources known from CGI. a. transport the CGI environment variables, the contents of the standard input (for POST) or the standard output (for the output to the client). Several clients can be served at the same time, as they can be differentiated based on the request ID; therefore, in contrast to CGI, only one program instance is required to serve many clients.

Web links