Nginx and its workers with some random facts

Nginx Processes

I have a VM with one CPU only and there I run #nginx 1.18.0 in ubuntu server. It involves with some lightweight work like static content serve and as well as dynamic content with load balancer capability.

If I check the command:

# ps -ef --forest | grep nginx | grep -v color
root 578518 1 0 12:49 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 578519 578518 0 12:49 ? 00:00:00 _ nginx: worker process
www-data 578520 578518 0 12:49 ? 00:00:00 _ nginx: cache manager process
www-data 578521 578518 0 12:49 ? 00:00:00 _ nginx: cache loader process

gives me some interesting facts.

  1. It has 1 master process.
  2. it has 1 worker process.
  3. It has 1 cache manages process.
  4. It has 1 cache loader process.

What are these processes?

  • master process performs some specific operations such as reading configuration from disk, bind ports and create child processes.
  • worker processes do all of the work we ask nginx to do to handle the traffics and similar things like network connections, read and write content to disk, handle communications with upstream servers.
  • cache loader only runs on startup and load the disk-based caches into memory and then exits. So, If you check the process list when nginx is started some times back, then you won’t find this process.
  • cache manager process runs periodically and manage the cache cleanup, invalidating cache objects and similar things. It is scheduled and thus not always taking resources.

There are some default parameter in the configuration which acts ok in many cases.

  • With one CPU in this VM and with default configuration like worker_processes as auto. Which is saying a number 1 with available CPU core.
  • Each worker process manages multiple connections simultaneously in a non blocking way. This approach minimizes the need for context switches, enhancing performance.
  • Worker processes are single-threaded and operate independently. This means each one can grab new connections and process them without waiting for others.
  • Despite their independence, worker processes can communicate with each other through shared memory. This is particularly useful for accessing shared cache data, session persistence information, and other shared resources.

Of Course depending on specific use case, tweaking those default values are possible to configure.