CGI -- 通用网关接口

定义:
  The CGI defines the abstract parameters, known as meta-variables,which describe a client's request. Together with a concrete programmer interface this specifies a platform-independent interface between the script and the HTTP server.
通用网关接口定义了服务器与脚本之间进行交互的参数和接口。

how does it work?
  写段程序叫做CgiImplementor实现接口。
现在打开mysite.net/index.php 于是发生下面过程:
  浏览器 ---发送请求--> 服务器 --接受请求并调用CgiImplementor程序实现的接口--> CgiImplementor --调用php解释器执行php脚本并生成内容--> 服务器 --CgiImplementor返回的内容->浏览器

实例:

Nginx+FastCGI运行原理

Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。这就是Nginx+FastCGI的整个运作过程,如图1-3所示。




原文地址:https://www.cnblogs.com/zechau/p/3515478.html