Contents

Nginx

Parent-master-child architecture

ResponsibilityWho Handles It
bind() and listen() portNginx master
fork() workersNginx master
epoll_wait and accept() clientsNginx workers
Deciding which worker handles a connectionKernel, not Nginx master
# The same file descriptor (FD 24u) pointing to the same underlying socket.
nginx   504     root     24u  IPv4 ... TCP *:28327 (LISTEN)
nginx 50995 www-data     24u  IPv4 ... TCP *:28327 (LISTEN)
nginx 50996 www-data     24u  IPv4 ... TCP *:28327 (LISTEN)
  1. The master process bind(2) a port name to a socket and set the socket as passive by listen(2). The socket should get FD_CLOEXEC cleared for inheritable to the children.
  2. The master process fork(2) child workers which inherit the socket (file descriptor).
  3. All the workers epoll_wait for and accept(2) connections using the same shared socket

When a client connect(2) to the port, the kernel chooses which worker gets to accept(2) the connection, via a queue or wakeup mechanism (e.g. epoll/kqueue).