📄 fastcgi.htm
字号:
while(FCGI_Accept() >= 0) { printf("Content-type: text/html\r\n"); printf("\r\n"); printf("Hello world!<br>\r\n"); printf("Request number %d.", count++); } exit(0); }</PRE> <P> 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. </P> <P> The <TT>fcgi_stdio.h</TT> header file provides the FastCGI replacement routines for the C standard I/O library. The <TT>FCGI_Accept()</TT> routine accepts a new request from the Web server. </P> <H3> Migrating Existing CGI Programs </H3> <P> 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: </P> <P> <I>Initialize application;<BR> </I> <TT>while(FCGI_Accept() >= 0) {</TT><BR> <I>Process request</I>;<BR> <TT>}</TT> </P> <P> 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. </P> <P> After migration, developers can clean up their FastCGI applications for best performance: </P> <UL> <LI> 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. </LI> <LI> 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. </LI> <LI> 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. </LI> </UL> <P> 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. </P> <H2> 5. FastCGI in the Open Market WebServer </H2> <P> This section describes the FastCGI support in the following Open Market server products: </P> <UL> <LI> Open Market WebServer V2.0 </LI> <LI> Open Market Secure WebServer V2.0 </LI> <LI> Open Market Secure WebServer (Global) V2.0 </LI> </UL> <P> For more information about FastCGI support, see the <I>Open Market WebServer Installation and Configuration Guide</I>. </P> <H3> Server Configuration </H3> <P> FastCGI applications are configured with the server's configuration file. Configuration has two parts. </P> <P> First, the server administrator defines an <I>application class</I>. For local applications, the application class specifies the details of running the FastCGI application, such as: </P> <UL> <LI> The pathname of the application executable. </LI> <LI> Any arguments and environment variables to pass to the process at startup. </LI> <LI> The number of processes to run. </LI> </UL> <P> 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. </P> <P> The second configuration step is mapping the application class to a role: </P> <UL> <LI> 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 <SPAN CLASS="c4">/rollcall/</SPAN> might be handled by the employee database application. </LI> <LI> For filter roles, the administrator configures a file extension to be handled by a filter application. For example, all files with the <SPAN CLASS="c4">.sql</SPAN> extension could be handled by a SQL query lookup filter. </LI> <LI> 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 <I>all</I> access control checks (possibly including multiple FastCGI authorizers) before access is allowed. </LI> </UL> <H3> Basic FastCGI </H3> <P> 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. </P> <P> The WebServer treats any file with the extension <SPAN CLASS="c4">.fcg</SPAN> 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. </P> <H3> Session Affinity </H3> <P> 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. </P> <P> To improve the effectiveness of this technique, the WebServer implements <I>session affinity</I>. 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).--> </P> <H2> 6. FastCGI Performance Analysis </H2> <P> 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. </P> <H3> FastCGI vs CGI </H3> <P> 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. </P> <TABLE BORDERCOLOR="#000000" BORDER="2"> <TR> <TD WIDTH="72"> <DIV CLASS="c5"> Static file </DIV> </TD> <TD WIDTH="180"> <DIV CLASS="c5"> 21ms + 0.19ms per Kbyte </DIV> </TD> </TR> <TR> <TD WIDTH="72"> <DIV CLASS="c5"> FastCGI </DIV> </TD> <TD WIDTH="180"> <DIV CLASS="c5"> 22ms + 0.28ms per Kbyte </DIV> </TD> </TR> <TR> <TD WIDTH="72"> <DIV CLASS="c5"> CGI </DIV> </TD> <TD WIDTH="180"> <DIV CLASS="c5"> 59ms + 0.37ms per Kbyte </DIV> </TD> </TR> </TABLE> <P> 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. </P> <P> 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: </P> <TABLE> <TR> <TD WIDTH="108"> CGI </TD> <TD WIDTH="331"> 59ms + 50ms + (0.37ms)(5) = 111ms </TD> </TR> <TR> <TD WIDTH="108"> FastCGI </TD> <TD WIDTH="331"> 22ms + (0.28ms)(5) = 23ms </TD> </TR> </TABLE> <P> 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.--> </P> <H2> 7. Conclusions </H2> <P> 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. --> </P> <H2> 8. For More Information </H2> <P> For more information about Open Market and our products, visit our Web site at:<SPAN CLASS= "c4">http://www.openmarket.com/</SPAN> </P> <P> 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:<SPAN CLASS= "c4">http://www.openmarket.com/fastcgi/</SPAN> </P> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -