迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

CGI初接触

作者:迹忆 最近更新:2022/11/13 浏览次数:

由于自己是一名PHP程序员,经常要搭建php的运行环境,现在比较流行的nginx+php的环境很受欢迎,而其所采用的模式是FastCGI的方式,所以花了一些时间了解了一下FastCGI。

CGI(Common Gateway Interface

在开始进入FastCGI之前我想先聊一聊其前身CGI。首先CGI是一个协议,它独立于编程语言,但是可以用编程语言来实现CGI——也就是CGI应用程序。在早先web 服务器只提供静态的内容给客户端,但是随著需求的增加,渐渐的最初的web服务器并不能满足这些需求,于是就产生了很多的技术来处理动态的内容,CGI就是这些需求下的产物。

那CGI应用程序究竟是如何工作的呢:

1. 对于每一个请求,服务器都会创建一个新的进程来处理这个请求。

2. Web 服务器使用环境变量来将请求信息传递给CGI程序进行处理。接着是用户交互的一些信息(比如说用户通过form表单提交的信息),web服务器将会把http头信息等传递给CGI的标准输入(stdin)由CGI程序进行处理。然后是输出信息写入标准输出文件(stdout)和错误信息写入标准错误文件(stderr)由CGI程序返回给web服务器,由服务器返回给客户端。(从这一点可以看出web服务器和CGI应用程序必须在同一台机器上)

3. CGI进程结束标志着当前请求的完成。

所以说CGI的工作原理图如下面所示

我们可以看到,当进程4终止以后请求4的请求也就结束了。使用CGI此种技术有其本身的优点。

1. 简单,对于开发人员来说此种方式很容易理解

2. CGI是一种协议,独立于开发语言,几乎可以用任何语言来实现

3. 进程独立,CGI应用程序是在独立的进程中运行的,而不是在Web服务器主进程中运行。此种方式使得CGI应用如果崩溃的话并不会影响Web服务

4. 最后,CGI的设计并不会受服务器架构的影响,它不会去关心服务器是单线程的还是多线程等等。

当然,事物总有其两面性,既然CGI有这些优点,那伴随而来的肯定也有其缺陷。最大的缺陷就是性能问题,因为对于每一个请求都会创建一个进程,当请求结束以后进程就会被杀掉回收,这样就使得性能有所下降。还有就是其第三个优点也正是其缺点,因为CGI进程是独立的,不会影响Web服务器进程,所以说它并不能连接到Web服务器的其它请求处理的阶段。

Server APIS

针对CGI的主要的性能问题,一些Web服务器已经集成了APIs来解决CGI的由于进程的创建和消失导致的性能下降的问题。使用API要比CGI运行的快,这是因为应用程序是在服务进程中运行的,并且此程序一直在请求资源。除此之外APIs提供了比CGI更多的功能,比如可以连接到web服务器的其它请求处理的阶段,这一点正是CGI所不具备的。

然而Server APIs的这些好处正是牺牲了CGI的优点才得以实现的。也就是说,Server APIs的劣势对应于CGI的优势。

首先APIs的实现是非常复杂的,其实现代价要比CGI提升了很多;其次APIs依赖于编程语言,应用程序必须由提供商的API支持的语言才能开发(通常使用的是C/C++)。像perl这种解释型的语言就得靠边站了,虽然说它很受CGI的欢迎;然后是其进程不是独立的,它的运行需要依赖于服务器的主进程,所以说存在bug的程序可能会降低web服务器的安全性能,反过来说存在bug的web服务器也可能会使应用程序崩溃;最后就是它和web服务器的架构结合的特别紧密,如果说web服务器是多线程的,那么API应用程序必须考虑线程的安全。如果web服务器是单线程的,那多线程的应用程序也就没有其用武之地了。并且如果服务器提供商改变了其web服务器的架构,那应用程序必须做出相应的改写。

例如PHP程序员经常使用的Apache+php的开发环境,PHP的解释器就是作为Apache的模块来运行的,也就是PHP程序是在Apache的进程下面执行的。而Apache是多线程的,所以说PHP必须考虑线程之间的安全。但是对于另一种web服务器nginx来说,nginx是单线程的,所以PHP就可以不用考虑线程之间的安全问题。所以说PHP的同一版本(在PHP5.3版本以前)一般情况下是有NTS(非线程安全)和线程安全两种的。

总结以上两种方式,各有其优点和缺点,而当前大多数web服务器使用了FastCGI的方式来进行请求处理,此种方式结合了CGI和APIs各自显著的优点。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Git installation and establishment of local warehouse service

发布时间:2025/04/05 浏览次数:89 分类:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

Docker daemon log location

发布时间:2025/03/24 浏览次数:66 分类:Docker

The Docker daemon provides essential information about the general state of your microservices architecture. Unfortunately, container-centric logging techniques allow you to collect relevant data from your services but provide little inform

Running Background Processes in Bash

发布时间:2025/03/23 浏览次数:82 分类:OPERATING SYSTEM

When you execute a command in the terminal, you need to wait for the command to finish executing. This is called the foreground process. However, some advanced programs need to run in the background. In Bash scripts, there is an easy way to

Bash waits for background processes

发布时间:2025/03/23 浏览次数:192 分类:OPERATING SYSTEM

This article explains how to wait for background processes in Bash. Bash waits for background processes The wait command in Bash can be used to wait for all background processes to complete. This command will wait for the process and return

Terminating a Process in Bash

发布时间:2025/03/22 浏览次数:136 分类:OPERATING SYSTEM

This article will first discuss the different concepts related to Linux processes. After this, we will learn the different ways to terminate a process. Before going into the kill command, we must understand some preliminary concepts. Simple

Stop a running process from a batch file

发布时间:2025/03/21 浏览次数:202 分类:OPERATING SYSTEM

This article explains how to stop a running process from a batch file in Windows. We use Batch's taskkill command to terminate the running process. 请注意 , the command will be executed only if the specified process is open. Batch file t

First contact with CGI

发布时间:2025/03/18 浏览次数:52 分类:NETWORK

Since I am a PHP programmer, I often have to build a PHP operating environment. The popular nginx+php environment is very popular, and the mode it adopts is the FastCGI method, so I spent some time to learn about FastCGI. CGI (Common Gatewa

Getting started with FastCGI

发布时间:2025/03/18 浏览次数:166 分类:NETWORK

In "First Contact with CGI", we mentioned the operating mechanisms of CGI and Server APIs, as well as their respective advantages and disadvantages. In this chapter, we will learn about FastCGI, which combines the advantages of CGI and Serv

使用 Windows PowerShell 启动进程

发布时间:2024/03/01 浏览次数:147 分类:编程语言

本文将解释 Start-Process cmdlet 是什么以及参数如何与 cmdlet 一起使用。本文还展示了使用 Start-Process cmdlet 的好处

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便