开发者

How does Nginx handle HTTP requests?

I u开发者_StackOverflownderstand 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).

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.

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 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜