First contact with CGI
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 Gateway Interface )
Before I get into FastCGI, I want to talk about its predecessor, CGI. First of all, CGI is a protocol that is independent of programming languages, but programming languages can be used to implement CGI, that is, CGI applications. In the early days, web servers only provided static content to clients, but as the demand increased, the initial web servers gradually could not meet these demands, so many technologies were created to handle dynamic content, and CGI was the product of these demands.
So how does a CGI application actually work?
1. For each request, the server creates a new process to handle the request.
2. The web server uses environment variables to pass the request information to the CGI program for processing. Then there is some information about user interaction (such as information submitted by the user through a form). The web server will pass the http header information to the standard input (stdin) of the CGI program for processing. Then the output information is written to the standard output file (stdout) and the error information is written to the standard error file (stderr). The CGI program returns it to the web server, and the server returns it to the client. (From this point, it can be seen that the web server and CGI application must be on the same machine)
3. The end of the CGI process marks the completion of the current request.
So the working principle diagram of CGI is as follows
We can see that when process 4 terminates, request 4 also ends. Using CGI has its own advantages.
1. Simple, this method is easy to understand for developers
2. CGI is a protocol that is independent of the development language and can be implemented in almost any language
3. Process independence: CGI applications run in independent processes, not in the main process of the Web server. This way, if the CGI application crashes, it will not affect the Web service.
4. Finally, the design of CGI is not affected by the server architecture. It does not care whether the server is single-threaded or multi-threaded, etc.
Of course, everything has its two sides. Since CGI has these advantages, it certainly has its disadvantages. The biggest disadvantage is the performance problem, because a process is created for each request, and when the request is completed, the process will be killed and recycled, which will reduce the performance. And its third advantage is also its disadvantage, because the CGI process is independent and will not affect the Web server process, so it cannot be connected to other request processing stages of the Web server.
Server APIS
In response to the main performance problem of CGI, some web servers have integrated APIs to solve the performance degradation caused by the creation and disappearance of CGI processes. Using APIs is faster than CGI because the application is run in the server process and the program is always requesting resources. In addition, APIs provide more functions than CGI, such as being able to connect to other request processing stages of the web server, which is exactly what CGI does not have.
However, these benefits of Server APIs are achieved at the expense of the advantages of CGI. In other words, the disadvantages of Server APIs correspond to the advantages of CGI.
First of all, the implementation of APIs is very complex, and its implementation cost is much higher than CGI; secondly, APIs rely on programming languages, and applications must be developed in the language supported by the provider's API (usually C/C++). Interpreted languages like perl have to stand aside, although it is very popular with CGI; then its process is not independent, and its operation depends on the main process of the server, so a buggy program may reduce the security performance of the web server, and conversely, a buggy web server may also cause the application to crash; finally, it is very closely integrated with the architecture of the web server. If the web server is multi-threaded, then the API application must consider thread safety. If the web server is single-threaded, then multi-threaded applications will have no use. And if the server provider changes the architecture of its web server, the application must be rewritten accordingly.
For example, PHP programmers often use the Apache+PHP development environment. The PHP interpreter runs as an Apache module, which means that the PHP program is executed under the Apache process. Apache is multi-threaded, so PHP must consider the security between threads. However, for another web server, nginx, nginx is single-threaded, so PHP does not need to consider the security issues between threads. Therefore, the same version of PHP (before PHP5.3) generally has two types: NTS (non-thread-safe) and thread-safe.
To sum up, the above two methods each have their own advantages and disadvantages. Currently, most web servers use the FastCGI method to process requests, which combines the significant advantages of both CGI and APIs.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Getting started with FastCGI
Publish Date:2025/03/18 Views:164 Category: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 启动进程
Publish Date:2024/03/01 Views:137 Category:编程语言
-
本文将解释 Start-Process cmdlet 是什么以及参数如何与 cmdlet 一起使用。本文还展示了使用 Start-Process cmdlet 的好处
在 PowerShell 中获取正在运行的进程列表
Publish Date:2024/02/29 Views:242 Category:编程语言
-
PowerShell Get-Process cmdlet 可用于检索 Windows 计算机中正在运行的进程列表。它提供有关每个正在运行的进程的详细信息。
在 C# 中检查进程是否正在运行
Publish Date:2024/02/03 Views:236 Category:编程语言
-
可使用两种主要方法检查进程是否在 C# 中运行:Process.GetProcessByName()函数和 Process.GetProcessById()函数。
在 C# 中启动一个进程
Publish Date:2024/02/03 Views:137 Category:编程语言
-
在本文中,我们将介绍在 C# 中启动进程的多种方法。本文将介绍在 C# 中启动进程的多种方法。
在 C++ 中用 Fork 创建进程
Publish Date:2024/01/02 Views:166 Category:C++
-
本文演示了如何在 C++ 中使用 fork 创建进程。本文将为大家讲解几种在 C++ 中使用 fork() 系统调用创建进程的方法。使用 fork() 在 C++ 程序中创建两个进程
杀死一个 Python 进程
Publish Date:2023/12/24 Views:134 Category:Python
-
本文讨论了杀死 Python 进程的三种方法。在使用 Python 编程时,有时我们的程序会陷入无限循环。
从批处理文件停止正在运行的进程
Publish Date:2023/08/12 Views:485 Category:操作系统
-
本文说明了如何从 Windows 中的批处理文件停止正在运行的进程。 我们使用 Batch 的 taskkill 命令来终止正在运行的进程。
IDLE的子进程在Python中没有出现连接错误
Publish Date:2023/07/04 Views:269 Category:Python
-
IDLE 代表集成开发和学习环境,是一个使用 tkinter 库用 Python 编码的 GUI。 它可在多个平台上运行,并具有与 Python 脚本配合使用的各种功能。