[转载]FastCGI: A High-Performance Web Server Interface (1)

本文档翻译英文技术原文文档来自:http://www.fastcgi.com/drupal/node/6?q=node/15 

Technical White Paper(技术白皮书)

FastCGI: A High-Performance Web Server Interface (FastCGI:一种高性能的WEB服务器接口)

April 1996


1. Introduction

The surge in the use of the Web by business has created a tremendous need for server extension applications that create dynamic content. These are the applications that will allow businesses to deliver products, services, and messages whose shape and content are in part determined by the interaction with, and knowledge of, the customers to which they are delivered.

This important movement away from static Web content is pushing the limits and exposing the weaknesses of the environment in which these applications are currently bound: CGI (Common Gateway Interface). Most importantly it does not offer the performance these applications require. A new communication infrastructure is needed to connect Web servers with these new applications. This is what led Open Market to develop FastCGI.  

FastCGI is a fast, open, and secure Web server interface that solves the performance problems inherent in CGI, without introducing the overhead and complexity of proprietary APIs (Application Programming Interfaces).

FastCGI是一种快速,开放,并且安全的WEB服务器接口用以解决CGI固有的性能问题。 不引用开销和复杂的专用的APIs(应用程序接口)。

This paper assumes that the reader has basic familiarity with Web technology and developing Web applications.

本文档假定读都已经熟悉基本的web技术和web应用程序开发技术。

  • Simplicity. It is easy to understand.
  • 简单性。这很容易理解。
  • Language independence. CGI applications can be written in nearly any language.
  • 独立于语言。CGI应用可以用任何语言写成。
  • Process isolation. Since applications run in separate processes, buggy applications cannot crash the Web server or access the server's private internal state.
  • 进程隔离。由于应用程序运行于独立的进程中,有严重BUG的应用程序不能引起web服务器崩溃或者访问服务器的内部状态。
  • Open standard. Some form of CGI has been implemented on every Web server.
  • 标准开放。CGI的某些格式已被实施在每个web服务器上。
  • Architecture independence. CGI is not tied to any particular server architecture (single threaded, multi-threaded, etc.).
  • 独立的架构。CGI不依赖于任何的服务器架构(单独线程,多线程,等等)。

 CGI also has some significant drawbacks. The leading problem is performance: Since a new process is created for each request and thrown away when the request is done, efficiency is poor.

CGI也有一些明显的缺憾。首要问题是执行:由于要为每个请求生成一个新进程,处理完成后再终止它。效率很差。

CGI also has limited functionality: It only supports a simple "responder" role, where the application generates the response that is returned to the client. CGI programs can't link into other stages of Web server request processing, such as authorization and logging.

CGI也有功能限制:它仅仅支持一个简单的“响应者”角色,由应用程序生成响应返回给客户端。CGI程序不能链接进入到web服务器请求处理的其它阶段。例如登录与验证。

Server APIs (服务器APIs)

In response to the performance problems for CGI, several vendors have developed APIs for their servers. The two most notable are NSAPI from Netscape and ISAPI from Microsoft. The freely available Apache server also has an API.

在回应CGI执行效能问题上,某些供应商开发了他们自己的服务器API。两个最显著的是API是来自Netcape的NSAPI和来自Microsoft的ISAPI。免费可用的Apache也有一个API。

 Applications linked into the server API may be significantly faster than CGI programs. The CGI startup/initialization problem is improved, because the application runs in the server process and is persistent across requests. Web server APIs also offer more functionality than CGI: you can write extensions that perform access control, get access to the server's log file, and link in to other stages in the server's request processing.

应用程序链接到服务器API也许会效率会比CGI程序更快,CGI的启动/初始化问题得到改善。因为应用程序运行在服务器进程中并且在服务器上是持续请求。WEB服务器APIs也比CGI提供的功能要多:你可以写扩展,执行访问控制,获得服务器的访问日志文件,还可以链接到服务器请求处理的其它阶段。

However, APIs sacrifice all of CGI's benefits. Vendor APIs have the following problems

然而,APIs牺牲了整个CGI的功效。供应商APIs有以下问题:

  • Complexity. Vendor APIs introduce a steep learning curve, with increased implementation and maintenance costs.
  • 复杂性。供应商APIs引入陡峭学习曲线,增加了实施和维护成本。
  • Language dependence. Applications have to be written in a language supported by the vendor API (usually C/C++). Perl, the most popular language for CGI programs, can't be used with any existing vendor API.
  • 语言独立。应用程序必须由供应商支持的语言编写(通常为C、C++)。PERL,最流行的CGI程序语言,不能被任何已存在的供应商API使用。
  • No process isolation. Since the applications run in the server's address space, buggy applications can corrupt the core server (or each other). A malicious or buggy application can compromise server security, and bugs in the core server can corrupt applications.
  • 非进和隔离。由于应用程序运行在服务器的地址空间,应用程序的严重BUG能引起核心服务器失效(或相互失效)。恶意的或者严重的应用程序BUG可以使服务器陷于不安全。并且核心服务器的BUG也能让应用程序中断。
  • Proprietary. Coding your application to a particular API locks you into a particular vendor's server.
  • 专有性。编码你的应用程序到一个专用的API就锁定你只能使用特定供应商的服务器。
  • Tie-in to server architecture. API applications have to share the same architecture as the server: If the Web server is multi-threaded, the application has to be thread-safe. If the Web server has single-threaded processes, multi-threaded applications don't gain any performance advantage. Also, when the vendor changes the server's architecture, the API will usually have to change, and applications will have to be adapted or rewritten.
  • 绑定服务器架构。API应用程序必须共享与服务器架构一样,如果WEB服务器是多线程,应用程序线程必须是安全的。如果WEB服务器是独立的线程处理,多线程应用程序不会获得任何性能上的优势。同样,供应商改变服务器架构,API通常不得不改变。而且应用程将必须适应这种改变或重写。

FastCGI

The FastCGI interface combines the best aspects of CGI and vendor APIs. Like CGI, FastCGI applications run in separate, isolated processes. FastCGI's advantages include:

FastCGI接品是绑定CGI和供商APIs的最佳组合。有点像CGI,FastCGI应用程序运行在单独,隔离的进程环境中,FastCGI的特点包括:

  • Performance. FastCGI processes are persistent-they are reused to handle multiple requests. This solves the CGI performance problem of creating new processes for each request.
  • 性能。FastCGI进程是持续的他们处理多个请求能重复的使用。这样就解决了CGI为每个请求创建新进程的执行效率问题。
  • Simplicity, with easy migration from CGI. The FastCGI application library (described on page 9) simplifies the migration of existing CGI applications. Applications built with the application library can also run as CGI programs, for backward compatibility with old Web servers.
  • 简单。方便从CGI迁移。FastCGI应用程序库简化了已存在CGI应用程序的迁移。跟应用程序构建在一起的应用程序库也能运行CGI程序,向后兼容旧的WEB服务器。
  • Language independence. Like CGI, FastCGI applications can be written in any language, not just languages supported by the vendor API.
  • 语言独立性。如同CGI,FastCGI应用程序可用任何语言编写,不仅仅是被供应商API所支持的语言。
  • Process isolation. A buggy FastCGI application cannot crash or corrupt the core server or other applications. A malicious FastCGI application cannot steal any secrets (such as session keys for encryption) from the Web server.
  • 进程隔离。一个有bug的FastCGI应用程序不会引发核心服务或者其它应用程序器崩溃或失败,恶意的FastCGI应用程序不能从WEB服务器上盗取机密(如加密会话密钥)
  • Non-proprietary. FastCGI is supported in all of Open Market's server products, and support is under development for other Web servers, including the freely available Apache and NCSA servers, as well as commercial servers from Microsoft and Netscape.
  • 非专用。FastCGI已被Open Market的服务器产品支持。而且还支持在其它WEB服务器下进行开发。包括名费的可用的Apache以及NCSA服务器,以及微软和网景的商业服务器。
  • Architecture independence. The FastCGI interface is not tied to a particular server architecture. Any Web server can implement the FastCGI interface. Also, FastCGI does not impose any architecture on the application: applications can be single or multi-threaded, regardless of the threading architecture of the Web server.
  • 架构独立性。FastCGI接口不绑定专有的服务器架构。任何WEB服务器都能运行实施FCGI接口,同样,FCGI没有在应用程序上强迫规定任何架构:应用程序能单进程或多线程,不会管WEB服务器是不是多线架构。
  • Support for distributed computing. FastCGI provides the ability to run applications remotely, which is useful for distributing load and managing external Web sites.
  • 支持分布式计算。FCGI提供了远程运行应用程序的能力,这对分布负载和管理外部WEB站点很有用。

The following sections describe the FastCGI interface, protocol, application library, and support in Open Market's WebServer products 

 下面的部份描术了FastCGI接品,协议,应用程序库,以及Open Market的WEB服务器产品支持。

2. FastCGI Interface

2.FastCGI接口

The functionality provided by the FastCGI interface is very similar to that provided by CGI. To best understand the FastCGI protocol, we review the CGI interface here. Basic CGI request processing proceeds as follows:

由于FastCGI接口提供的功能非常类似CGI接口。为了更好地了理解FastCGI协议。我们在这里回顾一下CGI接口。基本的CGI请求处理过程如下:

  1. For each request, the server creates a new process and the process initializes itself. 对于每个请求而言,服务器会创建一个新的进程并完成自身初始化。
  2. The Web server passes the request information (such as remote host, username, HTTP headers, etc.) to the CGI program in environment variables.WEB服务器传递请求给CGI程序的环境变量。(例如:远程主机,用户名,HTTP头,等)。
  3. The Web server sends any client input (such as user-entered field values from an HTML form) to the CGI program's standard input. WEB服务器传送任何客户端的输入到CGI程序的标准输入。(例如:用户在HTML表单域中输入的值)。
  4. The CGI program writes any output to be returned to the client on standard output. Error information written to standard error is logged by the Web server. CGI程序所有的输入信息写入标准输出并返回到客户端,错误信息写入标准错误日志并由WEB服务器记录。
  5. When the CGI process exits, the request is complete.完成请求的处理后,CGI程序就退出。

FastCGI is conceptually very similar to CGI, with two major differences:

FastCGI的概念与CGI很类似,主要有两方面的不同:

  • FastCGI processes are persistent: after finishing a request, they wait for a new request instead of exiting.
  • FastCGI处理是持续的:完成一个请求后,他们会等待新请求而不是退出。
  • Instead of using operating system environment variables and pipes, the FastCGI protocol multiplexes the environment information, standard input, output and error over a single full-duplex connection. This allows FastCGI programs to run on remote machines, using TCP connections between the Web server and the FastCGI application.
  • 而不是使操作系统环境变量与管道,FastCGI协议复用环境信息。一个单独的全双工连接上之上的标准输入,输出和错误。这就允许FastCGI程序运行在远程机器上,在各个WEB务器和应用程序之间使用TCP连接。

Request processing in a single-threaded FastCGI application proceeds as follows:

FastCGI应用程序在单独的线程中持续处理请求如下:

  1. The Web server creates FastCGI application processes to handle requests. The processes may be created at startup, or created on demand. Web服务器创建FastCGI应用程序处理请求。进程也许在开始时创建或根据需要创建。
  2. The FastCGI program initializes itself, and waits for a new connection from the Web server. FastCGI程序初始化自已从WEB服务器等待新的连接请求。
  3. When a client request comes in, the Web server opens a connection to the FastCGI process. The server sends the CGI environment variable information and standard input over the connection.当客户端请求到来时,WEB服务器打开一个连接让FastCGI处理。服务器在连接上传送CGI环境变量信息和标准输入。
  4. The FastCGI process sends the standard output and error information back to the server over the same connection. FastCGI处理发送来的信息再通过同一个连接将信息返回给标准输出和标准备错误。
  5. When the FastCGI process closes the connection, the request is complete. The FastCGI process then waits for another connection from the Web server. 当FastCGI处理完成请求,关闭连接。FastCGI进程等待从web服务器发来的其它连接请求。

FastCGI applications can run locally (on the same machine as the Web server) or remotely. For local applications, the server uses a full-duplex pipe to connect to the FastCGI application process. For remote applications, the server uses a TCP connection.

FastCGI应用程序可以运行在本地(同一台机器上的WEB服务器)也能运行在远程。对于本地应用程序服务器使用全双工管道连接到FastCGI应用程序,对于远程应用程序服务器使用TCP连接。

FastCGI applications can be single-threaded or multi-threaded. For single threaded applications, the Web server maintains a pool of processes (if the application is running locally) to handle client requests. The size of the pool is user configurable. Multi-threaded FastCGI applications may accept multiple connections from the Web server and handle them simultaneously in a single process. (For example, Java's built-in multi-threading, garbage collection, synchronization primitives, and platform independence make it a natural implementation language for multi-threaded FastCGI applications.)

FastCGI应用程序可以是单线程或多线程的。对于单线程应用程序,web服务器维护一个进程池处理客户端请求(如果应用程序是运行在本地),这个进程池的大小可由用户配置。多线程FastCGI应用程序可以同时接受来自web服务器的多个连接请并在同一个进程中同时处理它们。(例如:JAVA的内建的多线程,垃圾回收,派生同步,以及平台独立性使它成为一门自然的多线程FastCGI应用程序语言)。

Remote FastCGI 远程FastCGI

FastCGI's ability to run applications remotely (over a TCP connection) provides some major benefits. These benefits are described in this section, along with some of the security issues that affect remote FastCGI applications.

FastCGI的远程运行应用程序能力提供了某些好处(通过TCP连接)。这些好处及某些安全性问题及对FastCGI应用程序产生的影响在本节中描述。

FastCGI with Firewalls FastCGI与防火墙

Applications that run on organizational (external) Web servers and depend on internal databases can be a challenge to administer. Figure 1 shows a typical organization, with an external Web server, a firewall restricting access to the internal network, and internal databases and applications.

应用程序远程在组织的外部web服务器并且依赖内部的数据库对系统管理是种挑战。图1 显示了典型的组织架构,外部WEB服务器通过防火墙限制访问内部网络和内部数据库及应用。

With CGI and vendor APIs, the application has to run on the Web server machine. This means the server administrator has to replicate the necessary database information onto the system hosting the Web server (which may be difficult to do in an automated way without compromising firewall security). Or, the administrator may build a "bridge" that allows access through the Web server to internal databases and applications (which is effectively re-inventing remote FastCGI).

由于CGI和供应商APIs应用程序能在web服务器计算机上运行,这意味着服务器管理员不得不托管系统主机web务器必需的数据库信息。(要是没有防火墙安全折中办法这很可能是种很难做到的一种自动方法)或者,系统管理员也可以构建“桥梁”允许通过web服务器访问内部数据库和应用程序(这实际上是重新发明远程FastCGI)

With remote FastCGI, the applications can run on the internal network, simplifying the administrator's job. When used with appropriate firewall configuration and auditing, this approach provides a secure, high-performance, scalable way to bring internal applications and data to the external network.

 由于远程的FastCGI应用程序也能运行在内部网络,简化了系统管理员工作。当使用适当的防火墙配置与审计,这种方法提供了一个安全,高性能,可扩展,实现了内部应用程序和数据到外部网络的办法。

Load Distribution 分页负载

For resource-intensive CGI and API applications, the Web server machine quickly becomes the bottleneck for overall throughput. The usual way to solve this performance problem is to buy a bigger, faster Web server machine, or to partition the Web site across several Web servers.

对于资源密集型CGI和API应用程序,WEB服务器很快就变成整整个吞吐量的瓶颈。通常解决这个问题的方法就购买一个更大,更快的计算机。或都将web 站点分在计算机的几个分区上面。

With remote FastCGI, the resource-intensive applications can be moved off the Web server machine, giving the server administrator additional flexibility in configuring the Web server. The administrator can configure FastCGI applications "behind the scenes" without having to change any content links or the external view of the Web site. The administrator can use several smaller, inexpensive server machines for applications, and can tailor each machine to the application it is hosting.

对于远程FastCGI,资源密集型的应用程序可以移出web服务器,给予服务器管理员配置服务器更大的灵活性,管理员可以在“幕后”配置FastCGI应用程序不用改变任何连接内容或web站点的外部视图。系统管理员可以为应用程序使用几台小型的,廉价的机器,并能照每台机器分担应用承载

Security Issues with Remote FastCGI 远程FastCGI与安全问题

The two security issues with remote FastCGI connections are authentication and privacy. FastCGI applications should only accept connections from Web servers that they trust (the application library includes support for IP address validation). Future versions of the protocol will include support for applications authenticating Web servers, as well as support for running remote connections over secure transport protocols such as SSL or PCT.

远程FastCGI连接的两个安全问题是认证和隐私。FastCGI应用程序只能接受信任的Web服务器连接(应用程序库包括了IP地址验证)。该协议的未来版本将支持包括web服务器验证,以及远程运行在如安全传输协议层的上支持,例如:SSL或PCT。

The FastCGI Protocol FastCGI协议

This section offers a brief introduction to the protocol used on the connection between the Web server and FastCGI application. Most application developers will use the FastCGI application library and won't have to worry about the protocol details. However, specialized applications are free to implement the FastCGI protocol directly.

本部份提供了一种在web服务器和FastCGI应用程序之间连接协议的简要介绍,大多数应用程序开发员将使用FastCGI应用程序库而不必担心协议的详细细节。然而,专门的应用程序可直接通过FaastCGI协议自由实施。

FastCGI uses a simple packet record format on the connection between the application and the Web server. The same record format is used in both directions and is outlined in Figure 2.

FastCGI使用简单的包格式在web服务器和应用程序之间进行连接。同样的记录格式在两个方向上使用。(图1及图2官方网站打不开)

The protocol version field specifies the version of the FastCGI protocol that is in use. The type field specifies the type of the record (described in the following section). The request ID identifies this record to a particular request, allowing multiple requests to be multiplexed over a single connection. The data length field specifies the number of data bytes that follow.

协议版本字段指定FastCGI协议正在使用的版本。类型字段指定了记录的类型(描述在下面部分),请求ID标识这个记录是一个特别的请求,允许在一个单独连接上多路复用请求。数据长度字段在其后面指定数据字节长度号码

The different FastCGI packet types are:

 不同的FastCGI包类型有:

FCGI_PARAMS

Used for sending name/value pairs (CGI environment variables) from the Web server to the application. 用来从web服务器发送名字/值对到应用程序(CGI环境变量)

FCGI_STDIN Used for sending the standard input from the Web server to the application. 用于从web服务器发送标准输入到应用程序
FCGI_DATA Used for sending filter data to the application (for more information, see the filter role described on page 7.) 用来发送过滤数据到应用程序
FCGI_STDOUT Used to send standard output from the application to the Web server. 用来发送从应用程序到web服务器的标准输出
FCGI_STDERR

Used to send standard error information from the application to the Web server. 用来发送从应用程序到web服务器的标准错误。

FCGI_END_REQUEST

Ends the request (can be sent by either the server or the application).

结束请求

For complete protocol details, see the FastCGI Protocol Specification, available from the Web site listed at the end of this paper.

有关完整的协议细节,请参见FastCGI协议规范,或从本网站末尾列出的文章获得帮助。

Responder Role 响应作用

FastCGI's Responder role is identical to the functionality provided by CGI today. When a request comes into the server, the FastCGI program generates the response that's returned to the client (typically an HTML page).

FastCGI的响应者作用同今天CGI提供的功能相同,当有请求进入服务器,FastCGI应用程序生成响应并返回给客户端(通常是一个HTML页)

Filter Role 过滤器作用

The Filter role allows a FastCGI application to process a requested file before it is returned to the client.

过滤器的作用允许FastCGI应用程序在处理请求文件之前返回给客户端。

Let's assume that the Web server is configured so that all files with the .sgml extension are processed by a SGML-to-HTML FastCGI filter application, and the user accesses the following URL:

我们假设web服务器被配置成所有文件为.sgml的扩展名,以便使用FastCGI过滤应用程序SGML到HTML来处理。并且用户使用下面的URL访问。

/document.sgml

After the Web server makes an access control decision and maps this URL to a content file, it invokes the FastCGI filter application with this file available as input. The FastCGI program's HTML output is sent back to the client, just as in the responder role. The process is outlined in Figure 3.  

然后WEB服务器为这个URL使用访问控制决策和地图到一个内容文件,FastCGI过滤应用程序调用这个可用的文件作为输入,这个FastCGI程序以HTML输出发送给客户端,正如应答者角色一样。这个处理概述在图3中(图3没有)

Filter applications can significantly improve performance by caching filter results (the server provides the modification time in the request information so that applications can flush the cache when the server file has been modified).

过滤应用程序能大幅提高缓存过滤的成果(服务器在请求中提供修改时间,当服务器文件被修改使应用程序可以修改服务器缓存内容).

The Filter role is useful for:

过滤器的作用为:

  • On-the-fly format conversions 快速的格式转换
  • Dynamic documents (such as documents with embedded SQL queries, or dynamic advertisement insertion) 动态文档内容(例如与文档一起嵌入SQL查询,或动态插入广告)
  • Applying a standard template: headers, footers, and backgrounds 应用标准模版:页眉,页脚和背景

Authorizer Role 授权者角色

The Authorizer role allows a FastCGI application to make an access control decision for a request. The FastCGI application is invoked with all of the request information, just as in the Responder role. If the authorizer application generates a "200 OK" HTTP result, the Web server assumes that access is allowed and proceeds with the request. (The Web server may process other access checks, including other FastCGI authorizers, before access is ultimately allowed.) If the application generates any other response, that response is returned to the client and the request is ended. The response can be any valid HTTP response, including "Access Denied" or "Redirect". 

授权者角色允许为FastCGi应用程序为制定请求访问控制。FastCGI应用程序的调用和所有请求信息,有点像响应者角色。如果授权器应用程序生成"200 OK"的HTTP结果。WEB服务器允许请求继续访问。(WEB服务器可以处理其它访问检查,包括其它FastCGI认证,最终允许访问)。如果应用程序生成其它响应,那么响应会返回给客户端并且请求结束。响应也可以是任何有效的HTTP响应,包括"Access Denied"或"Redeirect".

The Authorizer role is useful for:授权者角色的好处为:

  • Access control based on username and password, where the user information is looked up in an external database.基于用户名和密码的访问控制,用户信息放在外部数据库进行查询.
  • Complex access policies, such as time-of-day based access.复杂的访问规则,例如:基于时刻的访问控制)
  • Smart-card challenge/response authentication.响应认证/智能卡挑战
  • Dynamic redirects, where the user is sent to different pages based on the request profile.动态重定向,基于请求属性向用户发送不同的页面。

4. FastCGI Application Library. FastCGI应用程序库

Open Market has developed a FastCGI application library that implements the FastCGI protocol (hiding the protocol details from the developer). This library makes implementing FastCGI programs as easy as writing CGI applications. 

Open Market 已经开发了一个FastCGI应用程序库来实现FastCGi协议(为开发者隐藏协议细节).这个库具有编写CGI应用程序一样开发FastCGi程序.

The application library provides a replacement for the C language standard I/O (stdio) routines, such as printf() and gets(). The library converts references to standard input, standard output, and standard error to the FastCGI protocol. References to other files "fall through" to the underlying operating system standard I/O routines. 

    应用程序库提供了替换C语言标准I/O(stdin)的子例程,例如printf()和gets(),这个库会转换引用标准输入,标准输出,和标准错误到FastCGI协议.引用操作系统底层I/O子例程其它文件会“失效"

This approach has several benefits: 

这种方法有几个好处:

  • Developers don't have to learn a new API to develop FastCGI applications.开发者无须再学习新的API开发FastCGI应用程序。
  • Existing CGI programs can be migrated with minimal source changes (CGI migration is described in more detail in the following section).已存在的CGI程序很小改变可以移植(CGI移植描述的更多细节在下面的部分中)
  • FastCGI interpreters for Perl, Tcl, and other interpreted languages can be built without modifying the interpreter source code.FastCGI解析程序为perl,TCL和其它解析语言不用修改解析器源代码来构建应用程序

Here's a simple FastCGI application:这里是一个简单的FastCGI应用程序:

#include  <fcgi_stdio.h>

void main(void)

{int count = 0;

 while(FCGI_Accept() >= 0) 

{

        printf("Content-type: text/html ");

            printf(" ");

        printf("Hello world!");

       printf("Request number %d.", count++);

}

      exit(0);

}

This application returns a "Hello world" HTML response to the client. It also keeps a counter of the number of times it has been accessed, displaying the value of the counter at each request. 

这个应用程序将向客户端 返回"Hello World" HTML应答。它保存一个每次访问的计数器计数器,用于显示每次请求的次数的值。

The fcgi_stdio.h header file provides the FastCGI replacement routines for the C standard I/O library. The FCGI_Accept() routine accepts a new request from the Web server. 

fcgi_stdio.h头文件提供FastCGi替换标准C I/O库的子例程。 FCGI_Accept()子例程从web服务器接受一个新的请求。

Migrating Existing CGI Programs 移植已存在的CGI程序.

The application library was designed to make migration of existing CGI programs as simple as possible. Many applications can be converted by adding a loop around the main request processing code and recompiling with the FastCGI application library. FastCGI applications have the following structure, with an initialization section and a request processing loop: 

应用程序库被设计成将已存在的CGI程序移植尽可能的简单,大多数应用程序可以被转换为主要的请求在一个循环中处理并与FastCGI库重新编译.FastCGI应用程序有下列结构,初始化部分与循环请求处理部份:

Initialize application;

初始化应用程序

while(FCGI_Accept() >= 0) {

Process request;请求处理

}

To ease migration to FastCGI, executables built with the application library can run as either CGI or FastCGI programs, depending on how they are invoked. The library detects the execution environment and automatically selects FastCGI or regular I/O routines, as appropriate. 

更容易移植到FastCGI.可执行文件构建的应用程序库能运行CGI或者FastCGI程序.取决于怎么调用他们。程序库检查执行环境并且适时自动选择FastCGI或是标准I/O例程库.

After migration, developers can clean up their FastCGI applications for best performance:

移植后,开发人员可以清理他们的FastCGI应用程序以获得最好的性能:

  • Fix any resource leaks. Many CGI programs do not attempt to manage memory or close files, because they assume the world is going to be cleaned up when they exit. (If you don't want to clean up your program, you can just have your process assume that it is leaking memory and exit after processing some fixed number of requests.) Purify from Pure Software is one of a number of excellent tools for finding leaks and other memory use problems.修复所有的源代码漏洞,大多数CGI程序不尝试管理内存和关闭文件,因为它们假定当他们退出后世界已经被清理。某些优秀的工具能找出内存溢出和其它内存使用问题。
  • Fix any problems with retained application state. The application must ensure that any state that it creates in processing one request has no unintended effects on later requests.保留应用程序状态修复所有问题。应用程序必须确保处理一个请求任何状态不能有意地影响以后请求.
  • Collapse functionality. A common practice with CGI applications is to implement many small programs, with one function per program. CGI encourages this, because smaller programs load faster. With FastCGI, it's better to have related functionality in a single executable, so there are fewer processes to manage and applications can take advantage of sharing cached information across functions.崩溃功能。通常的作法是CGI应用程序由多数小型程序构成,每个程序一个功能。CGI鼓励这样做,因为小型程序载入更快,由于FastCGI在一个单独可执行的环境中提供更好的相关功能。因此有较少的管理处理并且应用程序可以并行利用共享缓存信息.

Applications written in Perl, Tcl, and other scripting languages can be migrated by using a language interpreter built with the application library. FastCGI-integrated Tcl and Perl interpreters for popular Unix platforms are available from Open Market. The interpreters are backward-compatible: They can run standard Tcl and Perl applications.  

在perl,TCL或其它脚本语言中编写的应用程序能使用语言解析器构建能迁移的应用程序库。Open Market 为多数流行的Unix平台在FastCGI集成了可用的Tcl和perl解析器.解析器是向后兼容的。它们能运行标准的Tcl和perl应用程序。

5. FastCGI in the Open Market WebServer Open Market web服务器中的FastCGI

This section describes the FastCGI support in the following Open Market server products:

本节描述了Open Market服务器产品对FastCGI的支持如下:

  • Open Market WebServer V2.0
  • Open Market Secure WebServer V2.0
  • Open Market Secure WebServer (Global) V2.0

For more information about FastCGI support, see the Open Market WebServer Installation and Configuration Guide.

如要获得关于FastCGI支持的更多信息,请参见Open Market Web服务器安装和配置指南。

Server Configuration 服务器配置

FastCGI applications are configured with the server's configuration file. Configuration has two parts.

FastCGI应用程序通过服务器配置文件配置的.配置有两个部分。

First, the server administrator defines an application class. For local applications, the application class specifies the details of running the FastCGI application, such as:

首先,服务器管理员定义一个应用程序类。对于本地的应用程序,应用程序类指定了运行FastCGI应用程序的细节,例如:

  • The pathname of the application executable.可执的应用程序路径名。
  • Any arguments and environment variables to pass to the process at startup. 在启动时传递给进程的参数和环境变量
  • The number of processes to run.运行进程的号码

For remote applications, the class configuration information includes the host and TCP port to connect to. The Web server assumes that the FastCGI application has been started on the remote host. If a request comes in and the server can't connect to the FastCGI TCP port, the server logs an error and returns an error page to the client.

对于远程应用程序,类配置信息包括连接的主机和TCP端口,web服务器假定FastCGI应用程序已在远程主机上启动。如果一个请求到来并且服务不能连接到FastCGI TCP端口,服务器记录错误并返回一个错误页面给客户端。

The second configuration step is mapping the application class to a role:

第二个配置步骤是应用程序映像到一个角色:

  • For responder roles, the administrator configures some part of the URL space to be handled by the FastCGI application. For example, all URLs beginning with /rollcall/ might be handled by the employee database application. 响应者角色,管理员配置URL空间的某部分由FastCGI应用程序处理。例如,所有URL以 /rollcall/开始的可以被职员数据库应用程序处理。
  • For filter roles, the administrator configures a file extension to be handled by a filter application. For example, all files with the .sql extension could be handled by a SQL query lookup filter. 过滤器角色,管理员配置过滤应用程序处理文件扩展名。例如.所有文件为.sql的扩展名能被SQL查询过滤器处理.
  • For authorizer roles, the administrator configures an authorizer application in the same manner as other access methods (hostname, username/password, etc.) A request must pass all access control checks (possibly including multiple FastCGI authorizers) before access is allowed. 授权者角色。管理员配置一个授权者应用程序同其它访问是一样的.(主机名,用户名,密码等等).请求之前必须传递所有的访问控制检查,该访问才被允许(可能包括多个授权者)

Basic FastCGI FastCGI基本配置

To simplify migration for existing CGI programs, the WebServer provides a simple way to install new FastCGI programs without having to reconfigure the server. However, this approach doesn't offer all of the performance benefits of FastCGI application classes. 为了简化已存在的CGI程序迁移,web服务器提供一种简单方法来安装新的FastCGI应用程序而不用重新配置服务器。然而,这种方式不提供FastCGI应用程序类的所有性能优势。

The WebServer treats any file with the extension .fcg as a FastCGI application. When a request corresponds to such a file, the WebServer creates a new FastCGI process to handle the request, and shuts down the process when the request is complete (just as in CGI). In this mode of operation performance is comparable to CGI. Future versions of the WebServer will improve performance by automatically caching processes and re-using them for subsequent requests.

web服务器把文件扩展名为.fcg作为FastCGI应用程序处理。当一个请求对应于这样一个文件,web服务器创建一个新的FastCGI进程处理请求,当请求完成后就关闭进程(如同CGI一样).这种模式的操作性能与CGI相当。未来版本的web服务器将改善自动缓存进程和为再次请求重新使用它们以提高性能。

Session Affinity 会话的密切关系

FastCGI programs can improve performance by caching information in the application process. For applications that require frequent but expensive operations such as validating a username/password in an external database for each request, this technique can significantly improve performance.

FastCGI程序能在应用程序中处理缓存信息以改善性能。为了应用程序频繁需要,但是这种操作很费时间,例如为每个请求在外部数据确认用户名/密码,这种技术能大大改善性能

To improve the effectiveness of this technique, the WebServer implements session affinity. When session affinity is enabled, the WebServer arranges for all requests in a user session to be handled by the same FastCGI application process. What constitutes a "session" is configurable. The default configuration uses the WebServer's built-in session tracking facility to identify user sessions. However, the server administrator can use any part of the request information for the session affinity mapping: the URL path, the client's hostname, the username, etc. <!--Talk about applications that need to hold onto resources for the user (such as open connections to the database).-->

为改进这种技术有效性。web服务器实现了会话的密切关系.当会话的密切关系是启用的.web服务器为所有的请求安排同一个FastCGI应用程序处理用户会话.一个"session“配置由什么构成,默认配置使用web服务器内置会话追踪设备设备用户会话。然而,服务器管理员也能为会话的密切关系映射任何的请求部分信息:URL路径,客户端的主机名,用户名等.

6. FastCGI Performance Analysis FastCGI性能分析

How fast is FastCGI? The answer depends on the application. This section contains some real FastCGI performance measurements, as well as guidelines for estimating the FastCGI speedup.

FastCGI有多快?答案依赖于应用程序,本节包含了一些真实的FastCGI性能测试。即为指导又为FastCGI加速作估算.

FastCGI vs CGI

We measured the relative performance of CGI, FastCGI, and static files on the Open Market WebServer, using a simple application that generates a fixed number of output bytes. The following table shows the measured request processing time for different request types on a typical platform. The times are measured from the client perspective and include client, server, and application processing time.

我们测算CGI,FastCGI的相关性能,并在静态文件的Open Market web服务器上使用一个简单的应用程序生成固定的输出字节数,下表显示了在一个典型平台上为不同类型的请求测算请求处理时间。时间从客户端角度测算并且包括客户端,服务器端,以及应用程序处理时间.

Static file 21ms + 0.19ms per Kbyte
FastCGI 22ms + 0.28ms per Kbyte
CGI 59ms + 0.37ms per Kbyte

FastCGI performance is comparable to serving static files, and significantly better than CGI (clearly showing the high overhead for process creation). Real applications have an additional time component: process initialization, which should be added to overall request processing time.

FastCGI性能与服务的静态文件相当.显著的比CGI好(清楚地显示为创建进程的高开销).实际应用有一些额外的时间成份:进程初始化,都应该增请求处理时间。

Let's use this data to estimate the speedup from migrating a typical database CGI application to FastCGI. Assume the application takes 50ms to initialize the database connection and generates 5K of output data. Request performance can be computed as follows:

让我们迁移一个类型的数据库CGI程序到FastCGI程序来估算这个数据的加速比。假设应用程序要50ms初始化数据库连接及生成5K的输出数据。请求性能计算如下:

CGI 59ms + 50ms + (0.37ms)=111ms
FastCGI 22ms + (0.28ms)(5) = 23ms

In this example, FastCGI has a 5x performance advantage over CGI, mostly due to savings from not having to create and initialize new processes for each request.<!--Need to talk about FastCGI vs proprietary APIs.-->

在这个例子中,FastCGI超过CGI5倍的性能优势。主要归因于节省了为每个请求创建和初始化进程.

7. Conclusions 结论

Today's Web business applications need a platform that's fast, open, maintainable, straightforward, stable, and secure. FastCGI's design meets these requirements, and provides for a logical extension from proven and widely deployed CGI technology. This allows developers to take advantage of FastCGI's benefits without losing their existing investment in CGI applications.<!--Need to talk about NT.--> <!--Need to give "more punch" to this conclusion: include info about uses for FastCGI (accessing legacy data in databases, access control, distributed applications, apps that have to run in multiple OS environments. -->

今天的商业web应用程序需要一个快速,快速广,可维护,简单,稳定,安全的平台。FastCGI的设计满足这些要求,并且为本地扩展提供了广泛部属CGI的技术.这样允许开发人员不失去现有的CGI应用程序投资利用FastCGI的好处.

8. For More Information

For more information about Open Market and our products, visit our Web site at:http://www.openmarket.com/

For more information about the FastCGI protocol and the developer's kit, and the latest information about FastCGI standardization and support in other Web servers, visit the FastCGI project page at: http://www.fastcgi.com

This file is Copyright © 1996 Open Market, Inc.

from:http://fuzhong1983.blog.163.com/blog/static/1684705201002052599/

原文地址:https://www.cnblogs.com/junhwong/p/3487557.html