说道FastCGI 大家都觉得它比CGI的效率高 具体高在那?很多人会说CGI需要不断的fork 对系统压力大。
其实第一次接触FastCGI是在应用Nginx环境时候,今天再次理下FastCGI 到底是什么?
从官方(http://www.fastcgi.com)的About FastCGI
第一句话就告诉我们:
FastCGI is simple because it is actually CGI with only a few extensions.
FastCGI 其实是一些简单CGI的扩展
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.
如CGI ,FastCGI也具备语言无关系性。例如,FastCGI 提供一种的方法来提高Prel应用程序的处理能力。
既然是简单的CGI扩展那它的优势在那呢? 难道又是所谓的“重复造车”?
再看看官方的一句重要的解释
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.)
如CGI , FastCGI 独立运行于WEB服务器的核心进程。并提供大量的安全API。 后面的英文我觉得说的有点严肃。它告诉我们一个问题,FastCGI出现问题之后,以至于不会破坏WEB server的核心进程,一句话WEB server 还可以使用脱离FastCGI的应用。
简单的说就是某些核心应用的托管。 如 托管 Nginx的PHP 解析器进程。
spawn-fcgi.c的核心代码
static int fcgi_spawn_connection(char *appPath, char **appArgv, char *addr, unsigned short port, const char *unixsocket, int fork_count, int child_count, int pid_fd, int nofork);
static int find_user_group(const char *user, const char *group, uid_t *uid, gid_t *gid, const char **username);
最近真的要看看里面的代码。
Nginx的fast-cgi 源码在: src/http/modules/ngx_http_fastcgi_module.c 可以详细的研究下,再次鞭策自己。
FastCGI 的API文档:http://www.fastcgi.com/drupal/node/5