Nginx 如何处理 HTTP 请求? [英] How does Nginx handle HTTP requests?

查看:40
本文介绍了Nginx 如何处理 HTTP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 Apache 使用的线程驱动:每个连接打开一个线程,当发送响应时,该线程关闭,释放其他线程的资源).

I understand thread driven that Apache uses: every connection opens up a thread and when the response is sent, the thread is closed, releasing the resources for other threads).

但我没有得到 Nginx 使用的事件驱动设计.我已经阅读了一些关于事件驱动设计的基础知识 .. 但我不明白 nginx 如何使用它来处理 Web 请求.

But I don't get the event driven design that Nginx uses. I've read some basics about event driven design .. but I don't understand how this is used by nginx to handle web requests.

我在哪里可以阅读和理解 Nginx 如何以事件驱动的方式处理连接,以便我明白为什么它更好,而不是仅仅接受基于事件的设计比线程驱动的设计更好.

Where can i read and understand how Nginx is handling the connections in an event driven way so I get why it's better, rather than just accepting that event-based design is better than thread-driven design.

推荐答案

Nginx 使用 Reactor图案.基本上,它是单线程的(但可以分叉多个进程以利用多个内核).主事件循环等待操作系统发出就绪事件信号 - 例如该数据可从套接字读取,此时它被读入缓冲区并进行处理.单线程可以非常有效地为数以万计的并发连接提供服务(线程每个连接模型会因为巨大的上下文切换开销以及大量内存消耗而失败,因为每个线程都需要自己的堆栈).

Nginx uses the Reactor pattern. Basically, it's single-threaded (but can fork several processes to utilize multiple cores). The main event loop waits for the OS to signal a readiness event - e.g. that data is available to read from a socket, at which point it is read into a buffer and processed. The single thread can very efficiently serve tens of thousands of simultaneous connections (the thread-per-connection model would fail at this because of the huge context-switching overhead, as well as the large memory consumption, as each thread needs its own stack).

这篇关于Nginx 如何处理 HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆