Monday 16 December 2019

NGINX: Worker Processes


When you grep for nginx process, you will get below kind of output.

$ps -eaf | grep nginx
    0 64258     1   0 11:22AM ??         0:00.01 nginx: master process nginx
   -2 64784 64258   0 11:31AM ??         0:00.00 nginx: worker process
188003303 65280 26600   0 11:51AM ttys000    0:00.00 grep nginx

As you see above output, there are two processes.
a. Master Process
b. Worker Process

Master Process
It is the actual NGINX instance that we started.

Worker Process
It is the process spawned by master process to respond to client requests. Default number of worker processes for NGINX is 1.

How to change the number of worker processes?
Using worker_processes directive, you can change number of worker processes.

Syntax:
worker_processes number | auto;
Default:   
worker_processes 1;
Context:    main

Let’s confirm it by updating nginx.conf file.


nginx.conf
worker_processes 3;

events {
    
}

http {
    include mime.types;

    server {
        listen 9090;

        server_name localhost;

        location = /about{
            return 200 "Hello Welcome to NGINX";
        }

        location = /not_found{
            return 404 "File can't be found";
        }
    }
}


Validate and reload the configuration.
$sudo nginx -t
Password:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$
$sudo nginx -s reload


Grep for nginx process
$ps -eaf | grep nginx
    0 64258     1   0 11:22AM ??         0:00.01 nginx: master process nginx
   -2 66742 64258   0 11:57AM ??         0:00.00 nginx: worker process
   -2 66743 64258   0 11:57AM ??         0:00.00 nginx: worker process
   -2 66744 64258   0 11:57AM ??         0:00.00 nginx: worker process
188003303 66756 26600   0 11:57AM ttys000    0:00.00 grep nginx

As you see the output, there are three worker processes exist now.

How many worker processes I can configure to achieve maximum throughput?
To achieve more throughput, if your system has N cores, then configure N worker processes.

Using ‘worker_processes auto;’ option, we can spawn one worker process for each cpu core.


nginx.conf
worker_processes auto;

events {
    
}

http {
    include mime.types;

    server {
        listen 9090;

        server_name localhost;

        location = /about{
            return 200 "Hello Welcome to NGINX";
        }

        location = /not_found{
            return 404 "File can't be found";
        }
    }
}


Validate and reload the configuration.
$sudo nginx -t
Password:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$
$sudo nginx -s reload


Since I have 16 logical CPUS in my system. nginx spawn 16 worker processes.
$sysctl hw.physicalcpu hw.logicalcpu
hw.physicalcpu: 8
hw.logicalcpu: 16
$
$ps -eaf | grep nginx
    0 64258     1   0 11:22AM ??         0:00.02 nginx: master process nginx
   -2 67223 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67224 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67225 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67226 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67227 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67228 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67229 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67230 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67231 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67232 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67233 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67234 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67235 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67236 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67237 64258   0 12:21PM ??         0:00.00 nginx: worker process
   -2 67238 64258   0 12:21PM ??         0:00.00 nginx: worker process
188003303 67279 26600   0 12:21PM ttys000    0:00.00 grep nginx

Previous                                                    Next                                                    Home

No comments:

Post a Comment