Skip to content

Troubleshooting guide

Nginx "worker_connections are not enough": how to fix it

A worker process hit its connection limit. A reverse proxy uses two connections for each client request, so the default fills up sooner than people expect.

3 min read10 sectionsUpdated

On this page

Key points

  • Proxied requests need two connections.
  • The common default of 768 or 1024 is low.
  • File descriptors must match.
  • A rising count may mean slow clients or backends.

What it means

Each worker handles at most worker_connections at once, counting client connections and upstream connections. Past that, Nginx refuses new work, and users see connection failures or 500 errors. The theoretical capacity for a proxy is about workers x connections / 2.

Common causes

  • Real growth in traffic.
  • Slow backends keeping connections open longer.
  • Long-lived connections, such as WebSockets or long polling.
  • Slowloris-style clients.
  • No upstream keepalive.

Diagnose it

Enable stub_status on an internal address, and compare with the limit.

bash
# How often has it occurred?
sudo grep -c 'worker_connections are not enough' /var/log/nginx/error.log

# Limits in use
sudo nginx -T 2>/dev/null | grep -E 'worker_processes|worker_connections|worker_rlimit_nofile'

# Active, reading, writing, waiting
curl -s http://127.0.0.1/nginx_status

# File descriptor limit of a running worker
cat /proc/$(pgrep -f 'nginx: worker' | head -1)/limits | grep 'open files'

Fixes

Raise the limit, and raise descriptors with it:

worker_rlimit_nofile 65535; events { worker_connections 16384; }

Set worker_processes auto;. Enable keepalive to backends: keepalive 64; in the upstream, along with proxy_http_version 1.1; and an empty Connection header. Trim client_header_timeout and client_body_timeout to drop slow clients. Fix slow backends, since they are often the actual source of the pile-up. Reload with nginx -t && nginx -s reload, so that existing connections finish on the old workers.

What the status numbers mean

Active is all connections, including idle ones. Reading is connections sending their request, and a high number suggests slow clients. Writing covers requests being processed and responses being sent, and a high value suggests a slow backend or client download. Waiting is idle keep-alive connections, which are cheap and mostly harmless. A rise in Writing with traffic flat is a signature of backend slowness, and it is the reason a connection limit error can really be a database problem one layer away.

Catch it automatically

Alert when active connections exceed about 80 percent of capacity. Nginx monitoring charts those states over time, and performance tuning goes deeper.

Frequently asked questions

What should worker_connections be?

Several thousand or more is common. Remember descriptors must match.

Why does it count double?

Each proxied request has a client connection and an upstream connection.

Does keepalive use more connections?

It holds a few idle ones but saves creating new ones.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.