nginx worker_connections are not enough: tuning and keepalive
Fix nginx "worker_connections are not enough": raise worker_connections, fit worker_rlimit_nofile to the hard limit, check upstream keepalive (1.29.7+).
nginx can hold roughly worker_processes × worker_connections connections at once, and as a reverse proxy each request uses two: one to the client and one to the upstream. The real ceiling is the worker process's open file limit (RLIMIT_NOFILE). Check the error log for worker_connections are not enough and the workers' Max open files first, then raise both together.
[1] Check the current values. nginx -T prints the effective config with all included files.
$ nginx -T 2>/dev/null | grep -nE 'worker_processes|worker_connections|sendfile|tcp_nopush|keepalive_timeout|worker_rlimit_nofile'4:worker_processes auto;11: worker_connections 1024;25: sendfile on;26: #tcp_nopush on;28: keepalive_timeout 65;That is the official Docker image's nginx.conf. Directives you leave out fall back to module defaults:
| Directive | Module default | Official image nginx.conf |
|---|---|---|
worker_processes | 1 | auto (CPU count) |
worker_connections | 512 | 1024 |
worker_rlimit_nofile | none (inherited limit) | none |
keepalive_timeout | 75s | 65 |
sendfile | off | on |
The limit a worker actually got is in /proc/<worker PID>/limits. The 4-core test container had 4 workers with a soft limit of 1024:
$ for p in /proc/[0-9]*; do tr "\0" " " < $p/cmdline 2>/dev/null | grep -q "^nginx: worker" && grep "Max open files" $p/limits; doneMax open files 1024 524288 files Max open files 1024 524288 files Max open files 1024 524288 files Max open files 1024 524288 files On a regular server, find the PIDs with pgrep -f 'nginx: worker' and read the same file.
[2] Read the error log to see whether it is short. There are two levels. The test ran worker_processes 1 and worker_connections 8 with 10 connections attached:
[warn] 29#29: 8 worker_connections are not enough, reusing connections[alert] 29#29: 8 worker_connections are not enough| Log | Meaning |
|---|---|
[warn] … not enough, reusing connections | nginx closed idle connections (keepalive, waiting for a request) to make room; the new request was served (200 in the test) |
[alert] … not enough | Nothing to reclaim, so the new connection was dropped; the client sees a reset (curl exit code 56 in the test) |
Frequent warn lines mean keepalive connections are holding slots. alert means connections are being lost.
[3] Raise worker_connections. It lives in the events block and applies to new workers on reload. Back up the current file before editing:
$ sudo cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakThis is not the whole file, only the directives to change. Add or edit just these in your existing file; do not overwrite the file with this (… stands for the lines you keep, such as http { … }).
…worker_processes auto;worker_rlimit_nofile 16384;…events { worker_connections 4096;}…$ sudo nginx -t$ sudo nginx -s reloadSize it as "peak concurrent connections per worker × 2 if proxying". Per the docs, upstream connections count too. To roll back, copy the backup over, then nginx -t and reload.
[4] Match the open file limit. Raising only worker_connections makes nginx -t warn:
$ docker run --rm --ulimit nofile=1024:1024 -v ./big.conf:/etc/nginx/nginx.conf:ro nginx:stable nginx -t…nginx: the configuration file /etc/nginx/nginx.conf syntax is ok2026/10/27 11:20:43 [warn] 1#1: 4096 worker_connections exceed open file resource limit: 1024nginx: [warn] 4096 worker_connections exceed open file resource limit: 1024nginx: configuration file /etc/nginx/nginx.conf test is successfulWith worker_rlimit_nofile the workers raise their own limit and the warning goes away. But they cannot go above the hard limit. In a container with a hard limit of 1024, nginx -t passed and the workers still ended up like this:
[alert] 28#28: setrlimit(RLIMIT_NOFILE, 8192) failed (1: Operation not permitted)$ for p in /proc/[0-9]*; do tr "\0" " " < $p/cmdline 2>/dev/null | grep -q "^nginx: worker" && grep "Max open files" $p/limits; doneMax open files 1024 1024 files With a hard limit of 65536, the same config gave 8192:
$ for p in /proc/[0-9]*; do tr "\0" " " < $p/cmdline 2>/dev/null | grep -q "^nginx: worker" && grep "Max open files" $p/limits; doneMax open files 8192 8192 files So do not stop at a passing nginx -t; check the startup log for setrlimit and the workers' limits. Whatever starts nginx sets the hard limit: LimitNOFILE= for a systemd service (override with systemctl edit nginx), --ulimit nofile=<soft>:<hard> for a container.
[5] Check keepalive. On the client side, keepalive_timeout sets how long idle connections are held. If slots run short with many reusing connections warnings, lower it first.
The upstream side depends on the version. Since 1.29.7, upstream keepalive is on by default (32 idle connections per worker, local) and proxy_http_version defaults to 1.1. On 1.30.6 with only server in the upstream block, three requests reached the backend from the same client port (connection reused):
8081 35632 GET /a HTTP/1.18081 35632 GET /b HTTP/1.18081 35632 GET /c HTTP/1.1With keepalive 0; every request opened a new connection:
8081 35644 GET /a HTTP/1.18081 35650 GET /b HTTP/1.18081 35656 GET /c HTTP/1.1On versions before 1.29.7 (for example the Ubuntu 24.04 package, 1.24), turn it on yourself:
upstream app { server 127.0.0.1:8080; keepalive 16;}server { location / { proxy_pass http://app; proxy_http_version 1.1; proxy_set_header Connection ""; }}The keepalive number is not a cap on total connections; it is how many idle connections each worker keeps. The docs advise keeping it small so the upstream can still accept new connections.
[6] For heavy static traffic, turn on tcp_nopush on together with sendfile on (tcp_nopush only works with sendfile). If the same files are opened repeatedly, add a descriptor cache such as open_file_cache max=10000 inactive=30s;; cached descriptors count against the open file limit too. The listen queue (listen 80 backlog=…, 511 by default on Linux) is set together with the kernel's somaxconn (Linux somaxconn and backlog tuning). For the same symptom on Apache, see Apache MaxRequestWorkers reached.
Official references: Core functionality — worker_connections, worker_processes, worker_rlimit_nofile · ngx_http_upstream_module — keepalive (default changed in 1.29.7) · ngx_http_core_module — keepalive_timeout, sendfile, tcp_nopush, open_file_cache, listen backlog
Analyze your Nginx log in the browser ↗ — nothing is uploaded.