Question

Please help me understand what worker_processes and worker_connections are in Nginx and what is the relation between them. I have looked under Nginx directives it says:

worker_processes

A worker process is a single-threaded process.

If Nginx is doing CPU-intensive work such as SSL or gzipping and you have 2 or more CPUs/cores, then you may set worker_processes to be equal to the number of CPUs or cores.

If you are serving a lot of static files and the total size of the files is bigger than the available memory, then you may increase worker_processes to fully utilize disk bandwidth.

worker_connections

The worker_connections and worker_processes from the main section allows you to calculate max clients you can handle:

max clients = worker_processes * worker_connections

So I understand that worker_processes is single threaded and its value is helpful in CPU-intensive work, but I am unable to understand "allows you to handle max clients you can handle".

If anyone can give an example as given in worker_processes it would be helpful for me to understand.

Was it helpful?

Solution

worker_connections is the number of simultaneous connections; so they are simply stating how to calculate, for example:

  • you are only running 1 process with 512 connections, you will only be able to serve 512 clients.

  • If 2 processes with 512 connections each, you will be able to handle 2x512=1024 clients.

The number of connections is limited by the maximum number of open files (RLIMIT_NOFILE) on your system

nginx has a better, updated description of worker connections.

fyi, the wiki section is considered obsolete (dont ask), now only the main nginx.org/en/docs are preferred...

OTHER TIPS

  • Worker processes:

    • Nginx worker process that handles the incoming request.
    • Set this to worker_process auto; to automatically adjust the number of Nginx worker processes based on available cores.
    • This can go beyond the available cores if you have IO access.
  • Worker connections:

    • Each worker process can open by default 512 connections.
    • You can change this limit by worker_connections <no>.
    • You can set this to max limit ulimit -n.
    • hence,

    max_clients = worker processes * worker connections

From Nginx's Beginner’s Guide

nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests. nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. The number of worker processes is defined in the configuration file and may be fixed for a given configuration or automatically adjusted to the number of available CPU cores

This is how I understand it from Process and thread perspective. Worker_process is equivalent to process and worker_connection is equivalent to thread.

So a process(Worker_process) can have n number of threads(worker_connections).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top