The Common Gateway Interface (CGI) is a standard (seeRFC 3875: CGI Version 1.1) method forweb servers software to delegate the generation ofweb pages to executable files. Such files are known asCGI scripts; they are programs, often stand-alone applications, usually written in ascripting language.
让 web 服务器把产生 网页的 任务 指派给 可执行文件 这些可执行文件一般叫做 CGI脚本
A web server that supports CGI can be configured to interpret a URL that it serves as a reference to CGI scripts. A common convention is to have acgi-bin/ directory at the base of the directory tree and treat all executable files within it as CGI scripts. Another popular convention is to usefilename extensions; for instance, if CGI scripts are consistently given the extension.cgi, the web server can be configured to interpret all such files as CGI scripts.
In the case ofHTTP PUT or POSTs, the user-submitted data is provided to the program via thestandard input. In any case, according to the CGI standard, data is passed into the program using certain, specific environment variables. This is in contrast to typical execution, where command-line arguments are used and the environment is in constant upheaval and cannot be trusted. The web server creates a small and efficient subset of the environment variables passed to it and adds details pertinent to the execution of the program
Drawbacks
This "one new process per request" model makes CGI programs very simple to implement, but limits efficiency and scalability.
Calling a command generally means the invocation of a newly created process on the server. Starting the process can consume much more time and memory than the actual work of generating the output, especially when the program still needs to beinterpreted orcompiled. If the command is called often, the resulting workload can quickly overwhelm the web server.
The overhead involved in interpretation may be reduced by using compiled CGI programs, such as those inC/C++, rather than usingPerl or other scripting languages. The overhead involved in process creation can be reduced by solutions such asFastCGI, or by running the application code entirely within the web server using extension modules such asmod_php.
调用命令会在 server 上产生新的进程,进程的启动会消耗很多时间和内存,特别是但程序还要被解释 或编译 如果命令经常被调用,结果产生的负载会让web server负担很重
[edit]Alternatives
Several approaches can be adopted for remedying this:
- The popular Web servers developed their own extension mechanisms that allows third-party software to run inside the web server itself, e.g.Apache modules, NetscapeNSAPI plug-ins,IIS ISAPI plug-ins.
- Simple Common Gateway Interface or SCGI
- FastCGI allows a single, long-running process to handle more than one user request while keeping close to the CGI programming model, retaining the simplicity while eliminating the overhead of creating a new process for each request. Unlike converting an application to a web server plug-in, FastCGI applications remain independent of the web server.
- Replacement of the architecture for dynamic websites can also be used. This is the approach taken by solutions includingJava Platform, Enterprise Edition (a.k.a. Java EE), which runs Java code in a Java servlet container in order to serve dynamic content and optionally static content. This approach replaces the overhead of generating and destroying processes with the much lower overhead of generating and destroying threads, and also exposes the programmer to the library that comes with Java Platform, Standard Edition that the version of Java EE in use is based on.
The optimal configuration for any web application depends on application-specific details, amount of traffic, and complexity of the transaction; these tradeoffs need to be analyzed to determine the best implementation for a given task and time budget.