0
0
Nginxdevops~5 mins

Worker processes and connections in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a web server handles many visitors, it needs to manage how many tasks it can do at once. Worker processes and connections in nginx control how many users can be served at the same time without slowing down.
When your website gets slow because too many people visit at once
When you want to make sure your server uses all CPU cores efficiently
When you need to handle many connections without crashing
When tuning nginx for better performance on busy servers
When setting up nginx on a new server and want it to handle traffic well
Config File - nginx.conf
nginx.conf
worker_processes auto;
events {
    worker_connections 1024;
}
http {
    server {
        listen 80;
        server_name example.com;
        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

worker_processes auto; tells nginx to use as many worker processes as CPU cores available, making full use of the server's power.

worker_connections 1024; inside events sets how many connections each worker can handle at once.

This setup helps nginx serve many users efficiently by balancing work across CPU cores and limiting connections per worker.

Commands
This command checks the nginx configuration file for errors before applying changes. It helps avoid server crashes due to bad config.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server, so users don't experience downtime.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command shows running nginx processes, so you can verify the number of worker processes running.
Terminal
ps aux | grep nginx
Expected OutputExpected
root 1234 0.0 0.1 123456 2345 ? Ss 10:00 0:00 nginx: master process /usr/sbin/nginx www-data 1235 0.0 0.1 123456 2345 ? S 10:00 0:00 nginx: worker process www-data 1236 0.0 0.1 123456 2345 ? S 10:00 0:00 nginx: worker process
This command counts how many connections are open on port 80, helping you see how many users nginx is currently serving.
Terminal
sudo lsof -i :80 | wc -l
Expected OutputExpected
15
Key Concept

If you remember nothing else from this pattern, remember: worker_processes sets how many CPU cores nginx uses, and worker_connections sets how many users each worker can handle at once.

Common Mistakes
Setting worker_processes to a fixed low number like 1 on a multi-core server
This wastes CPU resources and limits how many users nginx can serve at the same time.
Use 'worker_processes auto;' to let nginx use all CPU cores automatically.
Setting worker_connections too low, like 512, on a busy server
Limits the number of simultaneous connections per worker, causing slowdowns or dropped connections.
Increase worker_connections to a higher number like 1024 or more based on expected traffic.
Not testing nginx config with 'nginx -t' before reloading
Bad config changes can cause nginx to fail to reload, leading to downtime.
Always run 'sudo nginx -t' to check config syntax before reloading.
Summary
Set worker_processes to 'auto' to use all CPU cores for better performance.
Set worker_connections to control how many connections each worker can handle.
Test nginx configuration with 'nginx -t' before reloading to avoid errors.
Reload nginx with 'systemctl reload nginx' to apply changes without downtime.
Check running worker processes and active connections to monitor server load.