CGI, FASTCGI PHP

CGI

What is CGI
CGI (Common Gateway Interface) is a standard protocol, which provides A standard protocol is created so that the server can run third-party programs like a command-line interface program, and these third-party programs can dynamically generate web pages. These third-party programs are called CGI scripts (programs that meet the CGI definition), or CGIs for short. As for how these CGI programs are executed is determined by the server. In general, CGI scripts can dynamically generate HTML upon receiving web requests.

Why CGI
Just as the browser sends the request information to the web server, the web server will also pass some necessary information to the CGI when it needs a CGI program program. Correspondingly, some information needs to be returned to the web server after the CGI program is run. This information includes some content in the HTTP response, such as the response status of the current request, and the type of content returned (eg HTML, PDF, or plain text) and so on.

A long time ago, different web servers used different methods to exchange information with CGI programs, which made CGI programs less versatile (CGI programs need to be modified accordingly according to different web servers) . So CGI was born, it defines some common methods to standardize the information exchange between web servers and CGI programs. Early CGI programs were mainly used to process HTML forms.

Connection between webserver and CGI program
In the web server, it is often possible to configure which URLs need to be processed by the CGI program. This is usually done by specifying that certain directories of the server belong to CGI programs (this directory corresponds to a certain form of url, such as “http://example.com/cgi-bin/pr…” This url corresponds to cgi- bin directory, so the server knows that this request needs to be processed by the CGI program)

The web server stores the necessary information in environment variables, and the CGI program obtains the necessary information from the environment variables, so Can realize the information exchange between the two. After the CGI program is processed, the information originally sent to the “standard output” will be transferred to the web server, and the server will return the result to the client.

Most of the following parameters are specified by the CGI standard and need to be passed to the CGI program by the web server (through the “environmental variables” mentioned earlier):

Server specific variables: 
SERVER_SOFTWARE: the name/version of the HTTP server
SERVER_NAME: the host name of the server (or IP address)
GATEWAY_INTERFACE: CGI/version.
Request specific variables:
SERVER_PROTOCOL: HTTP/version.
SERVER_PORT: TCP port.
REQUEST_METHOD: HTTP request method (GET, POST, etc.).
PATH_INFO: Path suffix
PATH_TRANSLATED: If PATH_INFO exists , This parameter represents the corresponding absolute path on the server.
SCRIPT_NAME: The corresponding path to the program (for example, /cgi-bin/script.cgi)
QUERY_STRING: The part after the "?" in the URL. These query strings usually appear in the form of "name=value" (for example, var1=val1&var2=val2...)
REMOTE_HOST: the host name of the client
REMOTE_ADDR: the ip address of the client.
AUTH_TYPE: authentication type (if available)
REMOTE_USER: related to AUTH_TYPE
REMOTE_IDENT: see ident, only if server performed such lookup.
CONTENT_TYPE: Internet media type of input data if PUT or POST method are used, as provided via HTTP header.
CONTENT_LENGTH: similarly, size of input data (decimal, in octets) if provided via HTTP header.
Other user agent related parameters ( Usually browser): HTTP_ACCEPT, HTTP_ACCEPT_LANGUAGE, HTTP_USER_AGENT, HTTP_COOKIE

Disadvantages of CGI
Every request must start a CGI program, as opposed to a request processing, start The performance consumption of the process accounts for a large proportion of the consumption of the entire process. Therefore, if a new CGI program needs to be started to process each request, it is obviously inefficient in performance.

CGI alternatives
Due to the shortcomings of CGI mentioned above, the following alternatives have appeared:

  1. fastCGI (“prefork” pre-generated);

  2. Modularization, run the corresponding program directly in the web server to achieve dynamic generation of html (for example, Apache’s mod_php);

  3. Use pre-compiled CGI programs (ie, compiled languages);

  4. Java servlet

  5. < /ol>

    FastCGI & php-fpm

    What is FastCGI
    FastCGI is a variant protocol developed on the CGI standard protocol. Its main goal is Reduce the load of the interaction between the web server and the CGI program, so that one server can handle more web requests at the same time.

    FastCGI implementation details
    Different from CGI, which starts a new CGI program every time it processes a request, FastCGI uses some resident CGI processes to process the source. Constant requests. These CGI processes are managed by the FastCGI management process (FastCGI server), not the web server. When receiving a web request, the web server sends some necessary information and the page request itself to the FastCGI process through Unix domain socket, or named pipe, or TCP connection (As for which CGI process is sent to is assigned by the FastCGI management process). Through the same connection method, the web response is returned to the web server. After the response is returned, the connection may be closed, but the web server and the CGI process that processes the request will continue to reside in memory, waiting to process the next request. Therefore, each CGI process can handle many web requests during its life cycle, instead of only one web request like CGI.

    What is php-fpm
    PHP-FPM (FastCGI Process Manager) is the specific implementation of FastCGI on PHP. Starting from PHP5.3.3, it has been integrated into PHP In the installation package.

    The connection between Apache and php

    1. CGI (basically no longer used)

    2. Modularity (mod_php)

    3. FastCGI

    For specific configuration, please refer to:
    https://segmentfault.com/q/10. ..
    http://php.net/manual/en/inst…

    How to connect nginx and php

    Usually use FastCGI method

    < p>Refer to the specific configuration
    https://segmentfault.com/a/11…
    http://php.net/manual/en/inst…

    References

    https://en.wikipedia.org/wiki…
    https://en.wikipedia.org/wiki…
    https://en.wikipedia.org /wiki…
    http://php.net/manual/en/inst…

Leave a Comment

Your email address will not be published.