web 服务相关

来源:互联网 发布:上海大学 刘铮 乐乎 编辑:程序博客网 时间:2024/06/08 09:13




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, wherecommand-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, NetscapeNSAPIplug-ins,IISISAPI 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 destroyingthreads, and also exposes the programmer to the library that comes withJava 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.



CGI应用程序的工作原理:

 它是运行在Web服务器上的一个程序,并由来自于浏览者的输人触发。CGI是在HTTP服务器下运行外部程序(或网关)的一个接口,它能让网络用户访问远程系统上的使用类型程序,就好像他们在实际使用那些远程计算机一样。

  1.浏览器通过HTML表单或超链接请求指上一个CGI应用程序的URL。
  2.服务器收发到请求。
  3.服务器执行指定所CGI应用程序。
  4.CGI应用程序执行所需要的操作,通常是基于浏览者输入的内容。
  5.CGI应用程序把结果格式化为网络服务器和浏览器能够理解的文档(通常是HTML网页)。
  6.网络服务器把结果返回到浏览器中。

编写语言

  CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。对初学者来说,最好选用易于归档和能有效表示大量数据结构的语言,例如UNIX环境中:
  · Perl (Practical Extraction and Report Language)
  · Bourne Shell或者Tcl (Tool Command Language)
  · PHP(Hypertext Preprocessor))
  由于C语言有较强的平台无关性,所以也是编写CGI程序的首选。
  Windows环境中:
  · C和C++
  由于Internet上大部分服务器使用的是UNIX操作系统,且几乎任一UNIX操作系统中都有Bourne Shell,因而后面讲述的例子中大部分是用Bourne Shell编写的。
  最终Perl由于其跨操作系统、易于修改的特性成为了CGI的主流编写语言,以至于一般的“cgi程序”就是Perl程序。

环境变量列表

  SERVER_NAME:运行CGI序为机器名或IP地址。
  SERVER_INTERFACE:WWW服务器的类型,如:CERN型或NCSA型。
  SERVER_PROTOCOL:通信协议,应当是HTTP/1.0。
  SERVER_PORT:TCP端口,一般说来web端口是80。
  HTTP_ACCEPT:HTTP定义的浏览器能够接受的数据类型。
  HTTP_REFERER: 发送表单的文件URL。(并非所有的浏览器都传送这一变量)
  HTTP_USER-AGENT:发送表单的浏览器的有关信息。
  GETWAY_INTERFACE:CGI程序的版本,在UNIX下为 CGI/1.1。
  PATH_TRANSLATED: PATH_INFO中包含的实际路径名。
  PATH_INFO:浏览器用GET方式发送数据时的附加路径。
  SCRIPT_NAME: CGI程序的路径名。
  QUERY_STRING:表单输入的数据,URL中问号后的内容。
  REMOTE_HOST:发送程序的主机名,不能确定该值。
  REMOTE_ADDR:发送程序的机器的IP地址。
  REMOTE_USER:发送程序的人名。
  CONTENT_TYPE:POST发送,一般为application/xwww-form-urlencoded。
  CONTENT_LENGTH:POST方法输入的数据的字节数。

有关CGI应用程序要注意的:

  CGI应用程序运行在浏览器可以请求的服务器系统上,执行时需要使用服务器CPU时间和内存。如果有成千上万的这种程序会同时运行,那会对服务器系统提出极高的要求。你要慎重考虑这个问题,以防止服务器系统崩溃。
  不完善的CGI应用程序可能成为别人非法进人服务器系统的通道,有可能导致重要的资料被删除或外泄。CGI应用程序主要的用途有以下几种:
  根据浏览者填写的HTML表单发送定制的答复;
  创建可单击的图像缩小图;
  创建一个浏览者可以搜索内容的数据库;
  提供服务器与数据库的接口,并把结果转换成HTML文档;
  制作动态HTML文挡。
  如果一个CGI脚本可以在每台计算机上做同样的事情;编写脚本就会变的很容易。不幸的是,CGI脚本依赖于服务器的操作系统,因此,对于非UNIX服务器来说,Prl(UNIX下编写脚本的一个常用工具)脚本毫无用处。所以,你必须定制安装你的CGI脚本。
  大多数服务器都提供CGI-BIN目录,但是这还不够。因为你应该拥有自己的CGI-BIN。这样,你就能运行自己的脚本(而不是让自己的系统去适应已存在于系统上的脚本)。因此,你的提供商应安装CGI-BIN,且能够帮助你编写脚本