5

My OS is Ubuntu, I use ps -aux |grep nginx, and I've found 3 nginx's processes; so my question is why there are 3 processes for nginx? it seems one process is by root, another two from www-data:

root      7833  0.0  0.0 126092  1476 ?        Ss   12:32   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

www-data  7834  0.0  0.0 126504  3124 ?        S    12:32   0:00 nginx: worker process

www-data  7835  0.0  0.1 126504  5068 ?        S    12:32   0:00 nginx: worker process
Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
Y W
  • 81
  • 1
  • 6
  • Run `nginx -g "master_process on; daemon on;"`, see [master process](https://nginx.org/en/docs/ngx_core_module.html#master_process) – Nick Dong Jun 07 '23 at 10:04

2 Answers2

5

The process that is being run as root is the master NGINX process. The two others are worker processes.

During the launch of NGINX service, the master process is the first one to launch. It spans off the worker processes that actually handle the connections.

The master process runs as root in order to be able to do things like binding to privileged network ports, reading TLS certificates/keys during configuration load.

The worker processes have dropped privileges, as they only require to be able to read website files.

The number of worker processes can be controlled with worker_processes configuration directive. The default value is 1. Which means on a system with default config you will see a total of 2 processes (1 master and 1 worker).

The more worker processes you have, the more connections your web server can handle on a multi-core system.

E.g. you have 4 core CPU. By setting worker_processes 4; you make sure that all cores are being used to handle connections, so it is going to improve performance on a busy website.

Moreover you can just set worker_processes auto;. That will have NGINX determine the number of logical CPU units and set the number of workers corresponding to that.

Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
2

The root process is necessary for nginx to access the network and files on your system.

The other two processes are set in your config file. Look there and you will see a setting for that which is dependent on the number of cores in the processor on your server. More available processes means more compute power as access to your server increases with visitors.

It's possible (I do not recall) that two processes is a default setting.

Rob
  • 14,746
  • 28
  • 47
  • 65