CGI and fastcgi

Source: Internet
Author: User

when we talk about CGI, what are we talking about?The oldest Web server simply responds to the HTTP request from the browser and returns the HTML file stored on the server to the browser, which is static HTML. Things are constantly evolving and websites are becoming more complex, so dynamic technology appears. But the server can not directly run php,asp such files, they can not do, outsourced to others, but to make a pact with the third, I give you what, and then you give me what, is to hold the request parameters sent to you, and then I receive your processing results to the client. The agreement is common Gateway Interface, or CGI. This protocol can be implemented with Vb,c,php,python. CGI is just an interface protocol, not a language at all. The following figure can see the process

Web server interacts with CGI programs

The Web server will determine how the data is routed to the CGI program based on the type of CGI program, typically passing data between the CGI programs through standard input/output streams and environment variables. As shown in the following:

The CGI program uses standard input (STDIN) and standard output (STDOUT) for input and output. The CGI program also uses environment variables to get input, and the operating system provides many environment variables that define the execution environment of the program, which the application can access. The Web server and CGI interface also set some environment variables to pass some important parameters to the CGI program. The CGI Get method also passes the data in the form to the CGI program through the environment variable query-string. Here are some common CGI environment variables:

Variable name Description
Content_Type The value of this environment variable indicates the MIME type of the information being passed. Currently, the environment variable content_type is generally: application/x-www-form-urlencoded, who says the data comes from an HTML form.
Content_length If the server and CGI program information is passed as post, this environment variable can be read from the standard input stdin bytes of valid data. This environment variable must be used when reading the input data.
Http_cookie The COOKIE content within the client.
Http_user_agent Provides customer browser information that contains a version number or other proprietary data.
Path_info The value of this environment variable represents the other path information immediately following the CGI program name. It often appears as a parameter to a CGI program.
Query_string If the server and CGI program information is passed in a get, the value of this environment variable even if the information is passed. This information is followed by the CGI program name, with a question mark '? ' in between. Separated.
Remote_addr The value of this environment variable is the IP address of the client sending the request, such as 192.168.1.67 above. This value is always present. And it is a unique identifier that Web clients need to provide to the Web server, which can be used in a CGI program to differentiate between different Web clients.
Remote_host The value of this environment variable contains the host name of the client that sent the CGI request. If you do not support the query, you do not need to define this environment variable.
Request_method Provides the method that the script is called. For scripts that use the http/1.0 protocol, only get and POST make sense.
Script_filename The full path of the CGI script
Script_name The name of the CGI script
server_name This is the host name, alias, or IP address of your WEB server.
Server_software The value of this environment variable contains the name and version number of the HTTP server that called the CGI program. For example, the value above is apache/2.2.14 (Unix)

An example

With all that said, you may feel bored, and writing a small program may be better understood. LIGHTTPD + CGI, write CGI program in C language.

LIGHTTPD Configure CGI, open cgi.conf, Cgi.assign = (". CGI" = "") to set the extension and interpreter for the CGI module. For the purposes of this statement, the CGI module's extension is ". CGI" and the CGI module does not require a special interpreter to execute. Because writing in C is an executable file.

Here is the TEST.C code:

#include "stdio.h" #include "stdlib.h" #include <string.h>int mian () {     char *data;     data = getenv ("query_string");     puts (data);     printf ("Hello cgi!");     return 0;}

Build the executable file into the directory of your server configuration program

GCC Test.c-o test.cgi

Access: Http://localhost/test.cgi?a=b&c=d results are:

A=b&c=dhello cgi!

Obtain the content submitted by the Get method through the environment variable "query_string", if you want to get the content of the post submission through getenv ("Content-length"), The Web server sets this environment variable when it calls a CGI program that uses the Post method, and its text value represents the number of characters in the input that the Web server transmits to the CGI program. The example above shows how the CGI program interacts with the Web server.

CGI and fastcgi

How CGI works: Whenever a client requests a CGI, the Web server requests the operating system to generate a new CGI interpreter process (such as Php-cgi.exe), and a CGI process quits after processing one request, and then creates a new process when the next request comes. Of course, this can be done in cases where there is little or no concurrency in the traffic. But when traffic increases and concurrency exists, this is not the way to go. So there is the fastcgi.

FastCGI is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it will not take a moment to fork once (this is the most notorious fork-and-execute mode of CGI).

  In general, the whole workflow of fastcgi is this:

Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) at 1.Web server startup

2.fastcgi The Process manager itself, start multiple CGI interpreter processes (visible multiple php-cgi) and wait for a connection from the Web server.

3. When a client request arrives at Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.

4. The FastCGI child process returns standard output and error information from the same connection to the Web Server after processing is complete. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.

php-fpm and spawn-fcgi

SPAWN-FCGI is a general-purpose fastcgi Management Server, which is part of LIGHTTPD, and many people use lighttpd spawn-fcgi for management in fastcgi mode. But there are shortcomings, so php-fpm is for PHP, a fastcgi implementation, he is responsible for managing a process pool to handle requests from the Web server. Currently, PHP-FPM is built into PHP.

Apache module mode

Remember once in XP configuration Apache + PHP, will be configured in Apache below paragraph:

LoadModule Php5_module C:/php/php5apache2_2.dll

When PHP needs to run under the Apache server, in general, it can be integrated in the form of modules, when the role of the module is to receive Apache sent over the php file request, and processing the requests, and then return the processed results to Apache. If we configure the PHP module in its configuration file before Apache starts, the PHP module registers the apache2 ap_hook_post_config hook and starts the module at Apache startup to accept requests for PHP files.

Apache hook mechanism refers to: Apache allows the module (including internal modules and external modules, such as mod_php5.so,mod_perl.so, etc.) to inject the custom function into the request processing loop. In other words, the module can join the Apache request processing process by hooking up its own processing function at any one of Apache's processing stages. Mod_php5.so/php5apache2.dll is the inclusion of the custom functions, through the hook mechanism into Apache, at all stages of the Apache processing process is responsible for processing PHP requests.

Some people test that NGINX+PHP-FPM may reach apache+mod_php5 5~10 times in high-concurrency situations, and now nginx+php-fpm use more and more.

CGI and fastcgi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.