FastCGI is a binary protocol for interfacing interactive programs with a web server. It is a variation on the earlier Common Gateway Interface (CGI). FastCGI's primary aim is to reduce the overhead related to interfacing between web server and CGI programs, allowing a server to handle more web page requests per unit of time.
History
Common Gateway Interface (CGI) is an interface specification allowing external applications to interact with web servers. CGI applications run in separate processes, which are created at the start of each request and destroyed at the end. This "one new process per request" model makes CGI programs simple to implement but limits efficiency and scalability. At high loads, the operating system overhead for process creation and destruction becomes significant, and the CGI process model prevents resource reuse methods such as reusing database connections or in-memory caching.
To address the scalability shortcomings of CGI, Open Market developed FastCGI and introduced it in their web server product in the mid-1990s. Open Market originally developed FastCGI in part as a competitive response to Netscape's proprietary, in-process application programming interfaces (NSAPI) for developing web applications. After being developed by Open Market, FastCGI was implemented by several other web server makers.
Implementation Details
Instead of creating a new process for each request, FastCGI uses persistent processes to handle a series of requests. These processes are owned by the FastCGI server, not the web server.
To service an incoming request, the web server sends environment variable information and the page request to a FastCGI process over either a Unix domain socket, a named pipe, or a Transmission Control Protocol (TCP) connection. Responses are returned from the process to the web server over the same connection, and the web server then delivers that response to the end user. The connection may be closed at the end of a response, but both the web server and the FastCGI service processes persist.
Each individual FastCGI process can handle many requests over its lifetime, thereby avoiding the overhead of per-request process creation and termination. Processing multiple requests concurrently can be done in several ways: by using one connection with internal multiplexing (multiple requests over one connection), by using multiple connections, or by a mix of these methods. Multiple FastCGI servers can be configured, increasing stability and scalability.
The separation of web applications from the web server in FastCGI has several advantages over embedded interpreters (such as mod_perl or mod_php). This separation allows server and application processes to be restarted independently, enables per-application security policies (an important requirement for ISPs and hosting companies), and allows different types of incoming requests to be distributed to specific FastCGI servers equipped to handle those types of requests efficiently.
Protocol Structure
Communication between web servers and FastCGI applications uses a simple record-based protocol. Records are structured with a header containing version, type, request ID, content length, and padding information. This design allows multiple requests to share a single connection and supports multiplexing different data streams (stdout, stderr) over the same connection.
Web Servers That Implement FastCGI
FastCGI is supported by many web servers, including:
- Apache HTTP Server — through mod_fcgid, mod_fastcgi (third-party), and mod_proxy_fcgi (added in Apache 2.4, supporting TCP FastCGI servers)
- Nginx — via the ngx_http_fastcgi_module
- Microsoft IIS — includes FastCGI component, commonly used with PHP
- Lighttpd — native FastCGI support
- Caddy — built-in FastCGI transport support
- Cherokee, Hiawatha, Jetty, OpenBSD httpd — various levels of support
Language Bindings
FastCGI is a protocol, not an implementation, so it is not tightly bound to any one language. APIs exist for C/C++, Java, Perl, PHP (via PHP-FPM), Python, Ruby, Lua, Go, Rust, Common Lisp, Haskell, and many others. Modern web frameworks such as Ruby on Rails, Catalyst, Django, and Plack allow use with either embedded interpreters or FastCGI.