CGI knowledge point

Source: Internet
Author: User
Tags rfc server port

CGI (Common Gateway Interface) is a protocol that allows web servers and CGI scripts to process customer requests together. Its Protocol definition document is http://www.ietf.org/rfc/rfc3875.

The Web server manages connections, data transmission, and network interactions. The CGI script is responsible for managing specific business logic.

The function of the Web server is to convert the client request (HTTP request) to a CGI script request, execute the script, and then convert the CGI script response to the client response (HTTP response ).

CGI script requests have two parts:Request metadata(Request meta-variables) and relatedMessage Body(Message-body ).

Request metadata

Includes:

                               "AUTH_TYPE" | "CONTENT_LENGTH" |                           "CONTENT_TYPE" | "GATEWAY_INTERFACE" |                           "PATH_INFO" | "PATH_TRANSLATED" |                           "QUERY_STRING" | "REMOTE_ADDR" |                           "REMOTE_HOST" | "REMOTE_IDENT" |                           "REMOTE_USER" | "REQUEST_METHOD" |                           "SCRIPT_NAME" | "SERVER_NAME" |                           "SERVER_PORT" | "SERVER_PROTOCOL" |                           "SERVER_SOFTWARE" | scheme |                           protocol-var-name | extension-var-name
 
Let's take a look at it one by one:
 
Auth_typeIt is a unique authentication method that identifies a user, such as basic and digest.
Content_lengthIs the length of the Request Message Body
Content_typeIs the format that identifies the message body
Gateway_interfaceIndicates the CGI version used, such as cgi/1.1.
Path_infoDescribes the address of the CGI script.
Path_translatedIs the accessible CGI path, which corresponds to the CGI script path, such
http://somehost.com/cgi-bin/somescript/this%2eis%2epath%3binfo
The corresponding path_info is/This. is. The. Path; Info
QUERY_STRINGRequest Parameter (the get parameter is included in this)
Remote_addrIdentifies the IP address of the client
Remote_hostIdentifies the domain name of the client.
Remote_identIndicates the user who sends the request. Most servers choose to ignore this attribute.
Remote_userIs the legal name of the user
Request_methodIs the request method, including get/post/Put/delete, etc.
Script_nameIs the virtual path of the script program, such as/test. php.
SERVER_NAMEIs the Domain Name of the Web Server
Server_portIs the Web server port name
Server_protocolIs the interaction request protocol between the Web server and the client.
Server_softwareThe identifier of the Response Web server sent to the client, such as nginx/1.0.6.
Request Message Body 
The request body of the client is directly forwarded, and the message body is placed in stdin and passed to the script
 
Pass related knowledge point parameters
The following problem occurs when the web server obtains an HTTP request, because the HTTP request involves methods such as get and post. How do I pass parameters to the executable program?
For example, in the get method, the CGI program obtains data from the environment variable QUERY_STRING.
What about post? The Web server uses stdin (standard input) to transmit data in CGI. The transmitted data length is placed in content_length.
For HTTP requests, QUERY_STRING stores http get parameters, and stdin stores HTTP body parameters.
 
The popular nginx + PhP method is to use nginx (Web Server) to convert requests into CGI requests to PHP-CGI, and then execute PHP-CGI processes, convert the returned value to CGI response and return it to nginx. Nginx then changes it into an HTTP Response and returns it to the client.
 
However, there is a problem here. cgi is a single process, and the life cycle of a process is only the stages of Request incoming, processing, and response. However, all Web servers need to accept multiple Web requests. Here, you need to enable Multiple CGI at the backend. Generally, the CGI server sets the number of CGI enabled.
 
Here we need to make it clear that CGI is different between the server and the client. The CGI client is placed on the Web server side. A Web server such as nginx and Apache has already implemented this client. The server needs to be restarted separately. If you use nginx + CGI + PHP, you need to start the PHP-CGI service. Of course, you can also think that such a service must be run in the background in the form of deamon, then, fork generates many CGI processes.
 
Reuse
Of course, some people may ask why the CGI process cannot be reused. FastCGI solves this problem. A process can process multiple requests. Of course, the speed will go up. Another type of CGI is scgi (simple CGI). scgi is similar to FastCGI. It can only be said that the protocol defined by scgi is simpler (so it is called simple ). The scgi client is written in C and the server is written in Perl.
For the most common nginx + CGI + PHP, it should be clear that the information obtained from $ _ server in PHP is actually obtained from CGI, of course, this is consistent with the client information obtained in nginx. In addition, Because CGI is different between the client and the server, it is easy to think that the CGI client needs to use TCP to connect to the client. Of course, each connection needs to occupy a port, so there are still port restrictions. From this point of view, it is not the case that the more CGI is enabled, the better (of course, the port limit of 6 W is far enough ).
 
Security
On the open cgi security issues, once the Bird brother broke a bug: http://www.laruence.com/2010/05/20/1495.html
Interested readers can take a look.
 
Is there a CGI server listening for ports? How can we prevent external network requests from executing CGI? The general method is to directly bind it to the IP address of 127.0.0.1 to ensure that only the local machine can access
Reference:
http://www.lyinfo.net.cn/webclass/cgi/default.htm
http://blog.csdn.net/ablo_zhou/article/details/3634954
http://www.ietf.org/rfc/rfc3875

CGI knowledge point

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.