CGI and FastCGI

Source: Internet
Author: User
When talking about CGI, we are discussing what earliest Web servers simply respond to HTTP requests from browsers and return HTML files stored on servers to browsers, static HTML. Things are always constantly evolving, and websites are becoming more and more complex, so dynamic technologies are emerging. However, the server cannot directly run php files, such as ASP files, but it cannot be done by itself. It can be outsourced to others, but it must make an agreement with the third party, what will I give you, and what will you give me, it means to send the request parameters to you, and then I receive your processing results to the client. This Convention is the Common Gateway Interface (CGI. This Protocol can be implemented using VB, C, PHP, and python. CGI is only an Interface Protocol and is not a language at all. The following figure shows the process.

 

Interaction between Web server and CGI program

The Web server determines how data is transmitted to the CGI program based on the type of CGI program. Generally, data is transmitted to the CGI program through standard input/output streams and environment variables. As shown in:

CGI programs use standard input (stdin) and standard output (stdout) for input and output. In addition, CGI programs get input through environment variables. The operating system provides many environment variables, which define the execution environment of the program and allow the application to access them. 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 transmits the data in form to the CGI program through the Environment Variable query-string. The following are some common CGI Environment variables:

Variable name Description
Content_type The value of this environment variable indicates the MIME type of the information passed. Currently, the environment variables content_type are generally application/X-WWW-form-urlencoded, which indicates that the data comes from the HTML form.
Content_length If the transfer method of the server and CGI program information is post, this environment variable is the number of bytes of valid data that can be read from the standard input stdin. This environment variable must be used to read the input data.
Http_cookie The cookie content in the client.
Http_user_agent Provides the client browser information that contains the version number or other proprietary data.
Path_info The value of this environment variable indicates other path information that follows the CGI program name. It is often used as a parameter of the CGI program.
QUERY_STRING If the information transmitted between the server and CGI program is get, the value of this environment variable is even if the information is transmitted. This information is followed by the CGI program name, and a question mark is used between the two '? .
Remote_addr The value of this environment variable is the IP address of the client sending the request, for example, 192.168.1.67 above. This value always exists. Moreover, it is the unique identifier that the Web Client must provide to the Web server. It can be used in CGI programs to distinguish different web clients.
Remote_host The value of this environment variable contains the host name of the client sending the CGI request. If you do not want to query, you do not need to define this environment variable.
Request_method Provides methods for calling scripts. For scripts using HTTP/1.0, only get and post make sense.
Script_filename Complete CGI script path
Script_name CGI Script Name
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 calls the CGI program. For example, the preceding value is Apache/2.2.14 (UNIX)

 

Example

After talking so much, you may feel bored. Writing a small program may be better understood. Lighttpd + CGI: use C to write CGI programs.

Configure CGI in Lighttpd, open CGI. conf, CGI. Assign = (". cgi" => ""), and set the extension and interpreter of the CGI Module. In this statement, the extension of the CGI Module is ". cgi" and the CGI module does not need a special interpreter for execution. Because C is used to write executable files.

The following 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;}

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

gcc test.c -o test.cgi

Access: http: // localhost/test. cgi? The result of a = B & C = D is:

a=b&c=dHello cgi!

Use the environment variable "QUERY_STRING" to obtain the content submitted by get. To obtain the content submitted by post, use getenv ("Content-Length "), the Web server sets this environment variable when calling a CGI program using the POST method. Its text value indicates the number of characters 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: every time a customer requests cgi, the Web server requests the operating system to generate a new cgiinterpreter (such as php-cgi.exe). a cgi process will exit after processing a request and create a new process when the next request comes. Of course, this can be done if there are few concurrent visits. However, when the access volume increases and concurrency exists, this method is not suitable. Then FastCGI is available.

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

  Generally, FastCGI's entire workflow is like this:

1. Load FastCGI Process Manager (iis isapi or Apache module) when the web server starts)

2.FastCGI Process Manager initializes itself, starts Multiple CGI interpreter processes (multiple PHP-cgi are visible), and waits for a connection from the web server.

3.When a client request arrives at the web server, the FastCGI process manager selects and connects to a CGI interpreter. The Web server sends CGI Environment Variables and standard input to the FastCGI sub-process PHP-CGI.

4After the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the web server. When the FastCGI sub-process closes the connection, the request processing is complete. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running on the Web server. In CGI Mode, PHP-CGI exits here.

PHP-FPM with spawn-fcgi

Spawn-fcgi is a common FastCGI management server. It is part of Lighttpd. Many people use spawn-fcgi of Lighttpd to manage FastCGI mode. However, there are disadvantages, so PHP-FPM is for PHP. FastCGI is an implementation that manages a process pool to process requests from Web servers. Currently, PHP-FPM is built in PHP.

Apache module Mode

I remember that I used to configure Apache + PHP in XP and configured the following section in Apache:

LoadModule php5_module C:/php/php5apache2_2.dll

When PHP needs to run on the Apache server, it can be integrated in the form of modules. At this time, the module is used to receive PHP file requests passed by Apache and process these requests, then, return the processed result to Apache. If the PHP module is configured in the configuration file before Apache is started, the PHP module registers the ap_hook_post_config hook of apache2 and starts this module at Apache startup to accept requests from the PHP file.

Apache's hook mechanism refers to the following: Apache allows modules (including internal modules and external modules, such as mod_php5.so and mod_perl.so) to inject custom functions into the request processing loop. In other words, the module can hook its own processing functions at any stage of Apache processing to participate in the Apache request processing process. Mod_php5.so/php5apache2. dll is used to inject the contained user-defined functions into Apache through the hook mechanism. It is responsible for processing PHP requests at each stage of the Apache processing process.

Some people test nginx + PHP-FPM in high concurrency may reach Apache + mod_php5 5 ~ 10 times, now more and more people use nginx + PHP-FPM.

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.