NGINX Worker Processes and Connections
Worker processes
When we start the NGINX service, we get a master process which we can view using systemctl. This master process then spawns worker processes that are used to serve requests. The default is one worker process.
We can change the no. of worker processes using the worker_processes
directive. But more workers do not necessarily mean more performance. Each NGINX process will serve as many requests as the hardware can support.
There are instances where there are benefits in increasing the no. of worker processes. On a multicore CPU, for example. A single NGINX process can only work on one core of a CPU. So say, we have an 8-core CPU. The optimal no. of worker processes would be 8. Do not run more than one process on one core.
You may check the no. of CPU cores in a Linux server using:
nproc
lscpu
Setting the worker_processes
directive to auto
will auto-increase the no. of processes to match the no. of cores on your server.
worker_processes auto;
No. of connections per worker
events {
worker_connections 1024;
}
The worker_connections
directive specifies the maximum number of connections that each worker process can handle. The default value is 512. However, you may want to increase the value of worker_connections
if your server is handling a lot of traffic.
For example, if you have a server with 4 GB of RAM, you might want to set worker_connections
to 1024. This will allow each worker process to handle up to 1024 connections, which can help to improve performance.
It is important to note that the total number of connections that NGINX can handle is limited by the number of open file descriptors that the system allows. You can check the maximum number of open file descriptors with the following command:
ulimit -n
1024
If the number of open file descriptors is lower than the value of worker_connections
, you will need to increase the value of ulimit
.
Request Capacity
Total no. of requests that can be served = worker processes * worker connections
This is very important for the performance optimization of NGINX.
Conclusion
By understanding the worker processes and number of connections settings in NGINX, you can optimize your server's performance and ensure that it can handle a large number of concurrent connections.
Subscribe to my newsletter
Read articles from Melvin C Varghese (melvincv) directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Melvin C Varghese (melvincv)
Melvin C Varghese (melvincv)
I am a DevOps Consultant. 3 years experience with Cloud technologies. 9 years total IT experience in the Linux, Networking and Data Visualization domains. Love listening to music and playing older PC games.