NGINX Architecture – Understanding the Architecture of NGINX

nginxscalabilityservertcp

According to this article, nginx has a master process and a number of worker processes. I'm trying to understand how a request is handled by the nginx worker processes. nginx uses an event driven architecture with multiple listen sockets and connection sockets.

Typically with an HTTP web server you'd have a single process listening on port 80. For a new connection the data for all requests would then go to port 80 via a socket e.g. (client-ip, client-port, server-ip, 80) where 80 is the server port. As I understand it you can only have a single process listening on a single port, so how exactly do these requests get forwarded to all the other ports nginx uses? Does the master process copy all the request and response data from local ports back and forth via port 80?

Thanks.

Best Answer

Each NGINX worker process is initialized with the NGINX configuration and is provided with a set of listen sockets by the master process.

The NGINX worker processes begin by waiting for events on the listen sockets - Inside Nginx Architecture

worker processes accept new requests from a shared "listen" socket - The architecture of open source projects

Basically only the master binds to port 80 (or whatever ports are configured). It opens a bunch of unix domain sockets, and shares the sockets with the worker processes. So the worker processes just spin in a loop accepting connections from the shared sockets and handling them. The special thing about unix domain sockets is they can be used to share open file descriptions (i.e. sockets) accross processes.

AFAIK this isn't the master "copying" requests for the worker. The HTTP connection is opened directly by the worker process from the socket and the request is read.

Related Topic