search

Home  >  Q&A  >  body text

web - Why do we need Nginx when we have CGI?

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 监听计算机的某一个端口(比如80), 等待用户的request

2. 远程有一个用户执行了一个request, Nginx监听到了, 然后把这个请求传给CGI程序(比如Python的WSGI)

3. CGI程序接受请求, 运行对应的代码, 然后返回一个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.

漂亮男人漂亮男人2748 days ago701

reply all(7)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 17:29:17

    Crab demon. A Zhihu style
    First fix the problem to 为什么CGI与WebServer不能互相替代? 因为CGI是一种标准,Nginx则是一种应用。两者不是同类,所以下面用WebServer代替Nginx.

    CGI是一种标准,Nginx is an application.
    File requested from 浏览器的角度来看,浏览器只负责发送请求,接收来自WebServer的返回结果并渲染之。对于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把包含了这些代码的文件发给浏览器, 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 动态语言。他们对数据库进行增删查改四大操作,并且返回结果给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应运而僧。CGI的定义是统一网关接口。从此WebServer收到后台动态交互请求就直接发给CGI,CGI发给动态语言,动态语言把结果发回给CGICGI再发回给WebServer, you know what happens next. . . .

    Then the conclusion is, CGI是一个翻译层,它的功能不是直接提供结果给浏览器,而是翻译来自WebServer的请求并转给后台的应用程序,并且把执行结果翻译成静态网页返回给WebServer, therefore, are not interchangeable.

    Finally, I wrote it in a hurry, and many of my expressions are not rigorous. Comments are welcome.

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 17:29:17

    • Load balancing
    • Reverse proxy
    • Smooth upgrade
    • Expansion of disaster recovery
    • Hide CGI language type
    • Record logs
    • gzip

    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

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 17:29:17

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 17:29:17

    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.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 17:29:17

    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.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 17:29:17

    Don’t you think that without Nginx, no one would be able to do point 1 of the 4 points you listed?

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 17:29:17

    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

    reply
    0
  • Cancelreply