← All posts
Guides· 6 min read

nginx worker_processes and worker_connections: how to tune them, with numbers

What worker_processes auto really does, how to size worker_connections from ulimit -n, the worker_rlimit_nofile trap, and how to tell you set them wrong.

By , Director of Softuvo Solutions (LinkedIn) · Published · Updated

Most nginx servers never need these two settings changed. The defaults are fine until you see a specific error. This guide explains what the two settings do, how to check whether you are hitting a limit, and what to change only if you are.

What the two settings do

nginx runs one master process and several worker processes. The workers handle the actual requests.

  • worker_processes is how many workers nginx starts.
  • worker_connections is how many connections each worker can have open at the same time.

Multiply them and you get roughly how many connections nginx can hold at once. If nginx is a reverse proxy, each request uses two connections, one to the client and one to your app, so halve that number.

worker_processes: use auto

Check what you have:

grep -E 'worker_processes|worker_connections' /etc/nginx/nginx.conf
nproc

worker_processes auto; starts one worker per CPU core, which is the right setting for almost every server. Most distro packages already ship with it. If yours has a fixed number lower than nproc, change it to auto. There is no benefit in running more workers than cores.

worker_connections: check before you change it

The default is 512, and 768 in the Debian and Ubuntu packages. You only need to raise it if nginx is actually running out. Here is how to tell.

First, look for the error nginx writes when it runs out:

sudo grep -E 'worker_connections are not enough|Too many open files' /var/log/nginx/error.log | tail
  • No lines: you are not hitting the limit. Do not change anything. If the site is slow, the cause is somewhere else, usually the app behind nginx.
  • "worker_connections are not enough": raise worker_connections (next section).
  • "Too many open files": the operating system limit is the problem, not nginx's setting (section after next).

Second, if you want to see how close you are, count open connections and compare with your capacity:

sudo ss -tn state established '( sport = :80 or sport = :443 )' | wc -l

If that number is well below worker_processes × worker_connections, you are fine.

If you see "worker_connections are not enough"

Raise the number in the events block of /etc/nginx/nginx.conf. 2048 or 4096 is plenty for most servers:

events {
    worker_connections 4096;
}

Then test the config and reload. reload does not drop current connections:

sudo nginx -t && sudo systemctl reload nginx

If you see "Too many open files"

Every connection uses a file handle, and Linux limits how many one process can have. Check the limit for a running worker:

grep 'open files' /proc/$(pgrep -f 'nginx: worker' | head -1)/limits

If that limit is smaller than worker_connections, raise it in /etc/nginx/nginx.conf, at the top level, outside any block:

worker_rlimit_nofile 65535;

Reload nginx and run the check again to confirm the new limit applied.

When changing these will not help

Slow pages, 502 and 504 errors are almost never caused by these settings. They usually mean the app behind nginx is slow or down. Check the app and its upstream errors in the nginx error log first; the 502/504 guide linked below walks through it.

Opservo reads your nginx worker_processes, worker_connections and open-files limit on every heartbeat and tells you if they do not fit together, with the values to set.

Read next in the application and service troubleshooting series: How to Reduce Nginx 502 and 504 Gateway Errors and How to Read and Make Sense of Nginx Error Logs. The whole series is at Application and service troubleshooting.