JIYIK CN >

Current Location:Home > Learning > NETWORK >

First contact with CGI

Author:JIYIK Last Updated:2025/03/18 Views:

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.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category: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

Publish Date:2025/03/24 Views:66 Category: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

Publish Date:2025/03/23 Views:82 Category: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

Publish Date:2025/03/23 Views:192 Category: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

Publish Date:2025/03/22 Views:136 Category: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

Publish Date:2025/03/21 Views:201 Category: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

Getting started with FastCGI

Publish Date:2025/03/18 Views:166 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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial