CGI details

When we talk about cgi, what are we talking about

  
   The earliest Web The server simply responds to the HTTP request sent by the browser and returns the HTML file stored on the server to the browser, that is, static html. Things are always evolving, and websites are becoming more and more complex, so dynamic technology emerges. But the server can’t run files like php and asp directly. You can’t do it yourself, so you can outsource it to others, but you have to make an agreement with the third, what I give you, and then what you give me, is to send the request parameters to you , And then I receive your processing results to the client. Then this agreement is the common gateway interface, or cgi for short. This protocol can be implemented with vb, c, php, python. Cgi is only an interface protocol, not a language at all. The following figure shows the process flow

WEB server and cgi program interaction

   The WEB server will determine the data transmission method to the CGI program according to the type of the CGI program , Generally speaking, through standard input/output streams and environmental changes Amount to transfer data between CGI programs. As shown below:

   CGI program is carried out through standard input (STDIN) and standard output (STDOUT) input Output. In addition, CGI programs also get input through environment variables. The operating system provides many environment variables, which define the execution environment of the program, and applications can access them. The Web server and CGI interface set some additional 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 commonly used CGI environment variables:

Variable name Description
CONTENT_TYPE The value of this environment variable indicates the MIME type of the information passed. At present, the environment variable CONTENT_TYPE is generally: application/x-www-form-urlencoded, which means that the data comes from an HTML form.
CONTENT_LENGTH If the server and CGI program information is transmitted by POST, this environment variable The number of bytes of valid data that can be read from the standard input STDIN. This environment variable must be used when reading the input data.
HTTP_COOKIE COOKIE content in the client.
HTTP_USER_AGENT Provide client browser information including version number or other proprietary data.
PATH_INFO The value of this environment variable represents other paths immediately after the CGI program name information. It often appears as a parameter of CGI programs.
QUERY_STRING If the server and CGI program information is passed by GET, the environment variable The value is even the information passed. This information is followed by the CGI program name, separated by a question mark’?’ between the two.
REMOTE_ADDR The value of this environment variable is the IP address of the client who sent the request, for example 192.168.1.67 above. This value always exists. Moreover, it is the unique identifier that the Web client needs to provide to the Web server, and it can be used in the CGI program to distinguish different Web clients.
REMOTE_HOST The value of this environment variable contains the host name of the client that sent the CGI request. If the query you want is not supported, there is no need to define this environment variable.
REQUEST_METHOD Provide the method by which the script is called. For scripts using HTTP/1.0 protocol, only GET and POST are meaningful.
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 of the HTTP server calling the CGI program and version number. For example, the above value is Apache/2.2.14(Unix)

An example

   said so much, You may be bored, writing a small program may be better understood. Lighttpd + CGI, write cgi programs in C language.

lighttpd configure cgi, open cgi.conf, cgi.assign = (“.cgi” => “”) set the extension and name of the cgi module Interpreter. As far as this sentence is concerned, it means that the extension of the cgi module is “.cgi” and the cgi module does not need a special interpreter to execute. Because what is written in c is an executable file.

Here is the code of test.c:

copy code< /span>
#include "stdio.h"#include "stdlib.h"#include << span style="margin:0px; padding:0px; color:rgb(0,0,255)">string.h>int main(){ char *data; data = getenv("QUERY_STRING"); puts(data); printf("Hello cgi!"); return 0;}
copy code

Generate executable files and place them in the directory of your server configuration program

gcc test.c -o test.cgi

Visit: http://localhost/test.cgi?a=b&c=d The result is:

a=b&c=dHello cgi!

   get the content submitted by get through the environment variable “QUERY_STRING”, if you want to get the content submitted by post, you can use getenv(“CONTENT-LENGTH”), the Web server sets this environment variable when calling the CGI program that uses the POST method. Its text value represents the number of characters in the input that the Web server sends to the CGI program. The above example shows the interaction between the cgi program and the web server.

cgi and fastcgi

   CGI working principle: Whenever a client requests CGI, the WEB server requests the operating system to generate a new CGI interpreter process (such as php-cgi.exe), and a CGI process exits after processing a request. Create a new process when the next request comes. Of course, this will work even when there are few visits and no concurrency. But when the amount of access increases and concurrency exists, this approach is not suitable. So there is fastcgi.

   FastCGI is like a long-live CGI, it can be executed all the time, as long as it is activated, it will not be executed every time It takes time to fork once (this is the most criticized fork-and-execute mode of CGI).

  Under normal circumstances, the entire workflow of FastCGI looks like this:

< p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(69,69,69); font-family:"PingFang SC","Microsoft YaHei",SimHei,Arial,SimSun; font-size:16px">   1.Load FastCGI process manager (IIS ISAPI or Apache Module) when Web Server starts

2.FastCGI process manager initializes itself and starts more A CGI interpreter process (multiple php-cgi can be seen) and wait for a connection from the Web Server.

3.when When a client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The Web server sends the CGI environment variables and standard input to the FastCGI subprocess php-cgi.

4 span>.FastCGI subprocess will return the standard output and error information from the same connection to the Web Server after finishing processing. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits and processes the next connection from the FastCGI process manager (running in the 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. Many people use Lighttpd’s Spawn-FCGI for management work in FastCGI mode. But there are disadvantages, soPHP-fpm is aimed at PHP, an implementation of Fastcgi, he is responsible for managing a process pool to handle requests from the Web server. Currently, PHP-fpm is built into PHP.

apache Module method

   remember that apache + php was configured in xp, and the following paragraph will be configured in apache:

< pre style="font-family:Monaco,Menlo,Consolas,"Courier New",monospace; font-size:13px; white-space:pre-wrap; padding:0px; margin-top:0px; margin-bottom:0px ; line-height:1.42857; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(245,245,245); border:1px solid rgb(204,204,204 )">LoadM odule php5_module C:/php/php5apache2_2.dll

   When PHP needs to run under the Apache server, generally speaking, it It can be integrated in the form of a module. At this time, the function of the module is to receive PHP file requests from Apache, process these 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 ap_hook_post_config hook of apache2 and starts this module when Apache starts to accept requests for PHP files.

Apache’s Hook mechanism means: Apache allows modules (including internal and external modules, such as mod_php5.so, mod_perl .so, etc.) inject custom functions into the request processing loop. In other words, the module can hook its own processing function in any processing stage of Apache to participate in the request processing process of Apache. mod_php5.so/ php5apache2.dll is to inject the included custom functions into Apache through the Hook mechanism, and is responsible for processing php requests at all stages of the Apache processing process.

Someone testnginx+PHP-FPM may reach Apache+mod_php5 is 5~10 times larger. Now nginx+PHP-FPM is used by more and more people.

–EOF

  

  The earliest Web server Simply respond to the HTTP request sent by the browser, and return the HTML file stored on the server to the browser, that is, static html. Things are always evolving, and websites are becoming more and more complex, so dynamic technology emerges. But the server can’t run files like php and asp directly. You can’t do it yourself, so you can outsource it to others, but you have to make an agreement with the third, what I give you, and then what you give me, is to send the request parameters to you , And then I receive your processing results to the client. Then this agreement is the common gateway interface, or cgi for short. This protocol can be implemented with vb, c, php, python. Cgi is only an interface protocol, not a language at all. The following figure shows the process flow

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

copy code

copy code

gcc test.c -o test.cgi

< pre style="font-family:Monaco,Menlo,Consolas,"Courier New",monospace; font-size:13px; white-space:pre-wrap; padding:0px; margin-top:0px; margin-bottom:0px ; line-height:1.42857; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(245,245,245); border:1px solid rgb(204,204,204 )">a=b&c=dHello cgi!

LoadModule php5_module C:/php/php5apache2_2.dll

Leave a Comment

Your email address will not be published.