After reviewing the information, I have a general understanding. Please correct me:
A website is running on a computer, with the following division of labor.
1. Nginx 監(jiān)聽計(jì)算機(jī)的某一個(gè)端口(比如80), 等待用戶的request
2. 遠(yuǎn)程有一個(gè)用戶執(zhí)行了一個(gè)request, Nginx監(jiān)聽到了, 然后把這個(gè)請求傳給CGI程序(比如Python的WSGI)
3. CGI程序接受請求, 運(yùn)行對應(yīng)的代碼, 然后返回一個(gè)response
Is the above understanding correct? If it is correct, why does Nginx exist? Because it seems that even without Nginx, it is possible to directly use CGI to accept requests. Is it just for load balancing?
Thanks.
Crab demon. A Zhihu style
First fix the problem to 為什么CGI與WebServer不能互相替代?
因?yàn)?code>CGI是一種標(biāo)準(zhǔn),Nginx
則是一種應(yīng)用。兩者不是同類,所以下面用WebServer
代替Nginx
.
CGI
是一種標(biāo)準(zhǔn),Nginx
is an application.
File requested from 瀏覽器
的角度來看,瀏覽器只負(fù)責(zé)發(fā)送請求,接收來自WebServer
的返回結(jié)果并渲染之。對于WebServer
來講,它需要做的僅僅是接收請求,尋找瀏覽器
and sent back. If only this were the case, the world would be perfect.
But everyone knows what happened next. . We not only have to browse static web pages, but we also have to log in to forums, post curse words, downvote answers and gain reputation, etc. These behaviors cannot be accomplished by static Html. So there are JS, Flash and other front-end based interactive technologies. WebServer
把包含了這些代碼的文件發(fā)給瀏覽器
, the latter parses it into what it should look like (or what it shouldn’t look like, such as IE6), and we can see animations on the page. These are called front-end interaction technologies.
But there are some interactive front-ends that cannot be done. For example, I posted a high-definition uncensored picture last time. I want to see everyone’s reactions, like the poster, what a good person, etc. Then this technology requires the use of a database, but the database itself is It requires another language to operate. This language can be python, prel, Ruby, PHP, etc. We call it 動(dòng)態(tài)語言
。他們對數(shù)據(jù)庫進(jìn)行增刪查改四大操作,并且返回結(jié)果給WebServer
, and the latter is passed to the browser.
Since there are many dynamic languages ??and many kinds of web servers, they are incompatible with each other, causing a lot of trouble for programmers. Well, CGI
應(yīng)運(yùn)而僧。CGI
的定義是統(tǒng)一網(wǎng)關(guān)接口。從此WebServer
收到后臺動(dòng)態(tài)交互請求就直接發(fā)給CGI
,CGI
發(fā)給動(dòng)態(tài)語言,動(dòng)態(tài)語言把結(jié)果發(fā)回給CGI
,CGI
再發(fā)回給WebServer
, you know what happens next. . . .
Then the conclusion is, CGI
是一個(gè)翻譯層,它的功能不是直接提供結(jié)果給瀏覽器,而是翻譯來自WebServer的請求并轉(zhuǎn)給后臺的應(yīng)用程序,并且把執(zhí)行結(jié)果翻譯成靜態(tài)網(wǎng)頁返回給WebServer
, therefore, are not interchangeable.
Finally, I wrote it in a hurry, and many of my expressions are not rigorous. Comments are welcome.
There are so many, I think after thinking about it I can list other benefits of nginx that are at least as long as the above
The communication between the browser and the web server is the HTTP protocol. The browser does not support the CGI/FastCGI protocol, so it is impossible to abandon Nginx and communicate directly with FPM, PHP-CGI, etc.
Nginx is essentially a web server. If you use CGI directly, then this CGI becomes a web server, and the logic is confusing.
CGI is for handling dynamic logic.
The web server is just an implementation of an HTTP service. It only receives a request and then replies with a corresponding response (usually an HTML page, or other files depending on the request), regardless of any logic. All logical processing is thrown to CGI. For example, user login verification, etc.
You can think of Nginx as a messenger. The main work is not done by him, but without him,
The people who actually do the work are the ones who go and pick up and hand over tasks in person.
It’s not that you can’t do it, it’s that the people who do the work only want to care about how the work is done well,
I don't want to do a lot of errands and make my functions a hodgepodge.
Don’t you think that without Nginx, no one would be able to do point 1 of the 4 points you listed?
Static files are basically handed over to nginx for processing.
For dynamic requests, nginx is equivalent to a layer of routing. You can go wherever you want. CGI only needs to focus on processing specific business logic