Nginx
Contents
Parent-master-child architecture
| Responsibility | Who Handles It |
|---|---|
bind() and listen() port | Nginx master |
fork() workers | Nginx master |
epoll_wait and accept() clients | Nginx workers |
| Deciding which worker handles a connection | Kernel, 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)- The master process
bind(2)a port name to a socket and set the socket as passive bylisten(2). The socket should getFD_CLOEXECcleared for inheritable to the children. - The master process
fork(2)child workers which inherit the socket (file descriptor). - All the workers
epoll_waitfor andaccept(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).