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:
1.web server starts loading the FASTCGI Process Manager (IIS ISAPI or Apache Module)
2. FASTCGI process Manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits 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 . 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 a fastcgi implementation of PHP, which manages a process pool to handle requests from the Web server. Currently, PHP-FPM is built into PHP.
nginx+fastcgi Operating principle
Nginx does not support direct invocation or parsing of external programs, and all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket under Linux (the socket can be either a file socket or an IP socket). In order to invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as the program used to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the FastCGI interface, the wrapper receives the request, and then derives a new thread, which invokes the interpreter or the external program to process the script and reads the return data; then wrapper returns the returned data Through the FastCGI interface, the fixed socket is passed to Nginx; Finally, Nginx sends the returned data to the client. This is the whole process of NGINX+FASTCGI's operation.
CGI and fastcgi