FastCGI
2010-09-27 13:10:02 阿炯

本站赞助商链接,请多关照。 FastCGI是一个程序接口,它能加速公共网关接口(CGI),CGI是一种用最常见的方式使Web服务器调用应用程序的Web应用程序。按一个 FastCGI工具来看,用户要求进入一个网站并使用一个专门的应用软件的话,使用FastCGI能够快3到30倍。FastCGI是Web服务器的一种插件,为了获得良好的性能,它要求对现有服务器应用程序(比如Perl 、Tcl 脚本和C、C++程序)做细小的改动。



FastCGI is simple because it is actually CGI with only a few extensions.

基本上,FastCGI 是一个在单一步骤中管理多重CGI请求的程序,为每个请求减少了许多程序指令。没有FastCGI的话,每当用户请求某一服务时都会导致Web服务器打开一个新的能控制和执行这项服务的程序,然后关闭它。有了FastCGI的话,一个步骤的耗费会被所有当前正处理的请求所分担。与CGI不同,有了 FastCGI 的话,每个步骤是独立于Web服务器运行的,这样就提供了更多的安全。FastCGI 是独立代码的,它的版权属于Open Market公司,该公司提供FastCGI的免费使用并且将其作为一个公开标准。FastCGI提供了唯一一个可以跨平台和在任何Web服务器上使用的无知识产权的方法。

从技术角度看,fastcgi的优点还是很多的,作为一种替代cgi的技术标准,它有如下优点:(稳定、安全、高性能、方便扩展)
*从稳定性上看,其是以独立的进程池运行来cgi,单独一个进程死掉,系统可以很轻易的丢弃,然后重新分配新的进程来运行逻辑。

*从安全性上看,其和宿主的server完全独立,fastcgi怎么down也不会把server搞垮。

*从性能上看,其把动态逻辑的处理从server中分离出来,大负荷的IO处理还是留给宿主server, 这样宿主server可以一心一意作IO,对于一个普通的动态网页来说,逻辑处理可能只有一小部分,大量的图片等静态IO处理完全不需要逻辑程序的参与。

*从扩展性上讲,其是一个中立的技术标准,完全可以支持任何语言写的处理程序(perl、php、java、python...)

* Like CGI, FastCGI is also language-independent. For instance, FastCGI provides a way to improve the performance of the thousands of Perl applications that have been written for the Web.

* Like CGI, FastCGI runs applications in processes isolated from the core Web server, which provides greater security than APIs. (APIs link application code into the core Web server, which means that a bug in one API-based application can corrupt another application or the core server; a malicious API-based application can, for example, steal key security secrets from another application or the core server.)

* Although FastCGI cannot duplicate the universality of CGI overnight, the FastCGI developers are committed to propagating FastCGI as an open standard. To that end, free FastCGI application libraries (C/C++, Java, Perl, Tcl) and upgrade modules for popular free servers (Apache, ISS, Lighttpd) are available.

* Like CGI, FastCGI is not tied to the internal architecture of any Web server and is therefore stable even when server technology changes. An API reflects the internal architecture of a Web server, so when that architecture changes, so does the API.

Not only does FastCGI restore the strengths of CGI, it also adds two new benefits:

* Distributed computing: Companies can run their FastCGI application on a different machine from the one on which they run their Web server. Distributed computing is a proven technique for scaling, linking to existing corporate systems, improving system availability, and improving security via compartmentalization, such as firewalls.

* Multiple and extensible roles: CGI applications compute the response to an HTTP request. FastCGI applications can do that and more, such as perform modular authentication and authorization checks and translate data from one type to another. FastCGI is designed so that more roles can be introduced in the future.

原来很多人都用 Lighttpd 的 Spawn-fcgi 进行 FastCGI 模式下的管理工作,不过有不少缺点。而 PHP-fpm 的出现多少缓解了一些问题,但 PHP-fpm 有个缺点就是要重新编译,这对于一些已经运行的环境可能有不小的风险(refer)。

问世 30 年后仍是反向代理的更优协议

技术博客作者 Andrew Ayer 于2026年4月下旬发文论证了一个反直觉的观点:诞生于 1996 年的 FastCGI 协议,在反向代理场景下仍然比当今主流的 HTTP 协议更加安全、可靠。文章标题直截了当 ——"FastCGI: 30 Years Old and Still the Better Protocol for Reverse Proxies"。

Ayer 的核心论点是,HTTP 作为反向代理与后端服务之间的通信协议,存在两个根本性的安全缺陷,而 FastCGI 通过其协议设计从根本上避免了这些问题。

第一个缺陷是消息分帧。HTTP 协议没有显式的消息边界标记,依赖解析器推断请求何时结束,这种歧义导致了臭名昭著的 "请求走私"(Request Smuggling)攻击。而 FastCGI 从设计之初就包含明确的消息长度字段,30 年来从未出现过类似的分帧漏洞。

第二个缺陷是可信数据的传递。当反向代理需要将客户端 IP、协议类型等信息传递给后端时,HTTP 只能依赖头部字段(如 X-Real-IP、X-Forwarded-For)。这些头部来自客户端请求,极易被伪造。FastCGI 则通过参数名前缀机制实现域隔离:所有来自客户端的头部统一加上 HTTP_前缀,而反向代理提供的可信数据(如 REMOTE_ADDR)则不加前缀,后端可以直接信任。

Ayer 在文中给出了 nginx 配置对比。HTTP 反向代理仅需一行 proxy_pass,而 FastCGI 配置稍长,需要 fastcgi_pass 和 fastcgi_params。但他认为这点配置复杂度完全值得,因为 "HTTP reverse proxying is a minefield"(HTTP 反向代理是一片雷区)。

文章也坦承了 FastCGI 的局限:不支持 WebSocket、工具生态较弱(curl 不直接支持)、部分场景下的吞吐量优化不如 HTTP 充分。但 Ayer 强调,他在生产环境使用 FastCGI 超过十年,态度很明确:"rather buy more hardware than deal with the nightmare of HTTP reverse proxying"(宁愿多买硬件,也不想面对 HTTP 反向代理的噩梦)。

这篇文章的启示在于技术选型不应盲目追随主流。当一个 30 年前的协议在核心安全属性上仍然优于今天的默认选择时,工程师值得重新审视自己的架构决策。

最新版本:2.4


官方主页:http://www.fastcgi.com/

该文章最后由 阿炯 于 2026-05-01 06:53:40 更新,目前是第 2 版。