nginx:它是多线程但使用多个进程? [英] nginx : Its Multithreaded but uses multiple processes?
问题描述
我试图了解是什么让 Nginx 如此之快,但我有几个问题.
I'm trying to understand what makes Nginx so fast, and I have a few questions.
据我所知,Apache 要么产生一个新进程来处理每个请求,要么产生一个新线程来处理每个请求.由于每个新线程共享虚拟地址空间,如果有大量并发请求进入,内存使用量会不断攀升.
As I understand it, Apache either spawns a new process to serve each request OR spawns a new thread to serve each request. Since each new thread shares virtual address space the memory usage keeps climbs if there are a number of concurrent requests coming in.
Nginx 通过只有一个监听进程(主)、一个执行线程和 2 或 3 个(数量可配置)工作进程来解决这个问题.这个主进程/线程正在运行一个事件循环.有效地等待任何传入的请求.当一个请求进来时,它会将该请求发送给其中一个工作进程.
Nginx solves this by having just one listening process(Master), with a single execution thread AND 2 or 3(number is configurable) worker processes. This Master process/thread is running an event loop. Effectively waiting for any incoming request. When a request comes in it gives that request to one of the worker processes.
以上理解有误请指正
如果以上是正确的,那么我有几个问题:
If the above is correct, then I have a few questions:
工作进程不会产生多个线程并遇到与 apache 相同的问题吗?
Isn't the worker process going to spawn multiple threads and going to run into the same problem as apache ?
或者是 nginx 速度快,因为其基于事件的架构在其底层使用非阻塞 IO.也许工作进程会生成仅非阻塞 IO 的线程,是吗?
Or is nginx fast because its event based architecture uses nonblocking-IO underneath it all. Maybe the worker process spawns threads which do only non-blocking-IO, is that it ?
究竟"是什么?是基于事件的架构",有人真的可以简化它,让像我这样的人理解.它是否仅与非阻塞 io 或其他内容有关?
What "exactly" is "event based architecture", can someone really simplify it, for soemone like me to understand. Does it just pertain to non-blocking-io or something else as well ?
我得到了 c10k 的参考,我我正在尝试通过它,但我不认为它与基于事件的拱门有关.似乎更适合非阻塞 IO.
I got a reference of c10k, I am trying to go through it, but I don't think its about event based arch. it seems more for nonblocking IO.
推荐答案
从概念的角度来看并不是很复杂.我会尽量说清楚,但我必须做一些简化.
It's not very complicated from a conceptual point of view. I'll try to be clear but I have to do some simplification.
基于事件的服务器(如 nginx 和 lighttpda>) 在事件监控系统周围使用包装器.例如.lighttpd 使用 libevent 来抽象更高级的高速事件监控系统(参见 libev 也是).
The event based servers (like nginx and lighttpd) use a wrapper around an event monitoring system. For example. lighttpd uses libevent to abstract the more advanced high-speed event monitoring system (see libev also).
服务器为每个连接使用一个简单的状态机来跟踪它拥有的所有非阻塞连接(包括写入和读取).当有新数据可用或可以写入更多数据时,事件监控系统会通知服务器进程.如果您了解套接字编程,则它就像一个增强的 select()
.服务器进程然后简单地使用一些高级函数发送请求的文件,例如 sendfile()
在可能的情况下,或者将请求转换为使用套接字进行通信的 CGI 进程(此套接字将使用事件监视系统进行监视就像其他网络连接一样.)
The server keeps track of all the non blocking connections it has (both writing and reading) using a simple state machine for each connection. The event monitoring system notifies the server process when there is new data available or when it can write more data. It's like a select()
on steroids, if you know socket programming. The server process then simply sends the requested file using some advanced function like sendfile()
where possible or turns the request to a CGI process using a socket for communication (this socket will be monitored with the event monitoring system like the other network connections.)
此链接提供了大量有关 nginx 内部结构的重要信息,以防万一.希望能帮到你.
This link as a lot of great information about the internals of nginx, just in case. I hope it helps.
这篇关于nginx:它是多线程但使用多个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!