When a traffic spike hits, Nginx rarely falls over because it is slow. It falls over because a limit somewhere runs out: file descriptors, worker connections, or memory for idle keepalive sessions. Tuning nginx concurrent connections means finding those limits before your users do, then raising them in the right order.
This guide walks through a practical setup for a single server handling around 10,000 simultaneous connections. The numbers are starting points, not guarantees. Your traffic pattern, response sizes, and hardware decide the final values, so every change here ends with a test.
Nginx Concurrent Connections: How the Limit Works
Nginx uses an event-driven model. A small number of worker processes can each hold many open connections at once, which is why one well-tuned box can serve thousands of visitors without the thread-per-connection cost of older servers. Each connection still costs a file descriptor and a slice of memory, and each worker has a ceiling set by worker_connections.
The useful formula is simple: total connections a server can accept roughly equals worker processes multiplied by worker_connections. With 4 workers at 4096 connections each, you get room for about 16,384 connections, which covers 10,000 with headroom. Good worker_connections tuning starts here, because the setting is meaningless if the operating system cannot supply the descriptors. The nginx event loop is what makes this model cheap: one worker waits on many sockets at once instead of dedicating a thread to each, so adding connections costs little CPU until descriptors or memory run out.
Before changing any Nginx setting, check the operating system limits. A worker cannot open more file descriptors than the system allows, so worker_connections above the nofile limit does nothing. This is the most common reason a tuned config still returns errors under load.
Set Worker Processes and Worker Connections
Start with workers. Set worker_processes to auto so Nginx matches your CPU cores, then set worker_connections in the events block:
worker_processes auto;
events {
worker_connections 4096;
use epoll;
multi_accept on;
}
On Linux, epoll is the efficient event method, and multi_accept lets a worker accept all queued connections at once instead of one at a time. For a direct comparison of how Nginx behaves against another server under heavy load, read our Nginx vs Apache high-traffic comparison, which covers where each server spends its resources.
Raise the system file descriptor limit to match. In /etc/security/limits.conf, or a systemd override for the Nginx service, set nofile to at least 65535. Then add worker_rlimit_nofile 65535; at the top of your Nginx config so workers inherit the higher ceiling. Restart Nginx and confirm with nginx -T that the values you expect are the values that loaded.
Tune Keepalive and Timeouts
At 10,000 connections, idle connections are your enemy. A browser that holds a keepalive connection open for a minute while the user reads a page occupies a slot that a new visitor cannot use. Keepalive still matters for performance, since it avoids a new handshake for every asset, but the timeout needs a sane cap.
keepalive_timeout 30;
keepalive_requests 100;
client_body_timeout 10;
client_header_timeout 10;
send_timeout 10;
reset_timedout_connection on;
A 30-second keepalive timeout with 100 requests per connection is a reasonable balance for most sites. Shorter timeouts free slots faster; longer ones help pages with many assets. Idle keepalive connections are the ones that quietly eat your headroom, so measure how many sit in the waiting state during peak before raising the timeout.
For proxied traffic, add upstream keepalive so Nginx reuses connections to your backend instead of opening a new one per request:
upstream app {
server 127.0.0.1:3000;
keepalive 64;
}
location / {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
This cuts backend connection churn, which is often the real bottleneck behind a slow proxy. Our guide to zero-downtime Nginx reverse proxy setups shows how this fits into a deployment that keeps serving traffic while you change configs.
Buffers, Caching, and the Rest of the Stack
Connection limits are only half the story. Large response buffers let a slow client hold a worker longer than needed, and missing cache headers force Nginx to fetch the same static asset from disk or upstream again and again. Set sensible buffers:
client_body_buffer_size 16k;
client_header_buffer_size 1k;
large_client_header_buffers 4 8k;
output_buffers 1 32k;
Serve static files with cache headers and gzip or Brotli compression. Compressed responses are smaller, so slow clients finish faster and free their connections sooner. If you terminate TLS at Nginx, enable session resumption and HTTP/2, which multiplexes many requests over one connection and reduces the total connection count in the first place.
Watch memory as you raise limits. Each connection holds buffers, so 10,000 connections with generous buffers can eat gigabytes. Monitor with stub_status or your existing monitoring stack, and watch the “active connections” and “waiting” counters during a load test rather than guessing from config values.
Test with Real Load Before You Trust the Config
Never ship tuning untested. Use a load tool from a separate machine, since testing from localhost hides network behavior. Start at 1,000 concurrent connections, watch error logs and response times, then step up to 5,000 and 10,000. Look for three failure signals: 502 and 503 responses, “too many open files” in the error log, and a climbing response time curve that suggests the backend, not Nginx, is saturated.
When the backend saturates first, scaling Nginx further will not help. At that point the fix is horizontal: more app servers behind a load balancer. If you are weighing that step, our comparison of Kubernetes ingress controllers including Nginx, Traefik, and HAProxy explains when an ingress layer makes sense and when a plain Nginx setup is enough.
Record the final values that passed your test, and keep the test command in your runbook. The next spike will not wait for you to rediscover them.
Quick Checklist
- Workers set to auto, worker_connections sized for 1.5x your target
- File descriptor limit raised above total worker connections
- Keepalive timeout capped, upstream keepalive enabled for proxied apps
- Compression and cache headers on for static assets
- Load test passed at target concurrency with clean error logs