本文档翻译英文技术原文文档来自:http://www.fastcgi.com/drupal/node/6?q=node/15 html
Technical White Paper(技术白皮书)node
FastCGI: A High-Performance Web Server Interface (FastCGI:一种高性能的WEB服务器接口)web
April 1996sql
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.promise
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应用程序开发技术。
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有如下问题:
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的特色包括:
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请求处理过程以下:
FastCGI is conceptually very similar to CGI, with two major differences:
FastCGI的概念与CGI很相似,主要有两方面的不一样:
Request processing in a single-threaded FastCGI application proceeds as follows:
FastCGI应用程序在单独的线程中持续处理请求以下:
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:
过滤器的做用为:
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:受权者角色的好处为:
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:
这种方法有几个好处:
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应用程序以得到最好的性能:
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的支持以下:
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应用程序的细节,例如:
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:
第二个配置步骤是应用程序映像到一个角色:
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/