Nginx handles huge numbers of connections efficiently with its default settings. When it sits in front of an application, a handful of settings make a measurable difference to latency, errors and server load.
Workers and connections
worker_processes auto; # one per CPU core
worker_rlimit_nofile 65535;
events {
worker_connections 4096; # per worker; total ≈ workers × connections
multi_accept on;
}
Each proxied request uses two connections (client and upstream), so plan for that when sizing. Raise the open-files limit to match; see too many open files.
Keep-alive to clients and upstreams
keepalive_timeout 65s;
keepalive_requests 1000;
upstream app {
server 127.0.0.1:3000;
keepalive 64; # idle connections kept open to the upstream
}
location / {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
Without upstream keep-alive, Nginx opens a new connection to your app for every request, adding latency and load. The app's own keep-alive timeout must be longer than Nginx's idle period, or you will see intermittent 502 errors.
Buffering
By default Nginx buffers upstream responses, freeing the app quickly even for slow clients. Keep it on for normal responses; turn it off only for streaming (Server-Sent Events, long polling) with proxy_buffering off on those locations. Raise proxy_buffer_size if large headers (big cookies) cause "upstream sent too big header" errors.
Compression
gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript image/svg+xml;
Brotli (via a module, or at a CDN) compresses text better still. Do not compress already-compressed formats such as JPEG, PNG or WOFF2.
TLS
Enable TLS 1.2 and 1.3, session resumption and OCSP stapling, and use HTTP/2. If a CDN terminates TLS in front of Nginx, these settings matter less for users but still affect CDN-to-origin performance. See HTTP/2 vs HTTP/3.
Static files and caching
Serve static assets directly from Nginx with sendfile on, long cache headers for hashed file names, and open_file_cache for busy sites. For dynamic pages that are the same for everyone, proxy_cache with a short TTL can absorb traffic spikes; see HTTP caching headers.
Timeouts
proxy_connect_timeout, proxy_read_timeout and proxy_send_timeout should reflect your app's real latencies and nest correctly with other layers; see timeouts in distributed systems and 504 Gateway Timeout.
Worked example
An API behind Nginx showed p99 latency 80 ms higher than the app itself measured. Nginx's upstream block had no keep-alive, so every request opened a new TCP connection to the app. Adding keepalive 64 with HTTP/1.1 and an empty Connection header removed most of the gap and cut the app's connection churn; raising the app's keep-alive timeout above Nginx's stopped a trickle of 502s.
Measuring the effect
Before and after each change, compare: request time and upstream time from JSON access logs, the 5xx share, active and dropped connections from stub_status, and p95 latency from APM. See Nginx monitoring; VitalRix includes a ready-made Nginx check that reads stub_status, and APM shows the app side.
Common mistakes
- No upstream keep-alive.
- Buffering off everywhere, tying up app workers with slow clients.
- Compressing images and fonts that are already compressed.
- Timeouts longer than the app's, so Nginx keeps clients waiting on dead requests.
Rate limiting at Nginx
limit_req_zone and limit_req give a simple per-IP or per-key rate limit at the edge, protecting the app from bursts and abusive clients:
limit_req_zone $binary_remote_addr zone=api:10m rate=20r/s;
location /api/ {
limit_req zone=api burst=40 nodelay;
limit_req_status 429;
proxy_pass http://app;
}
Remember that behind a load balancer or CDN, $binary_remote_addr is the proxy's address unless you configure the real IP module. See rate limiting strategies.
Changing configuration safely
Test every change with nginx -t before reloading, reload rather than restart (nginx -s reload keeps existing connections), change one thing at a time, and compare the access-log metrics before and after. Keep the configuration in version control, so a bad change can be reverted in seconds.
How many worker_connections do I need?
Enough for peak concurrent connections divided by workers, remembering that each proxied request uses two connections.
Should I enable proxy_cache?
For responses that are the same for everyone and can be slightly stale, yes. Never cache personalized responses without the user in the key.
Why do I get 502s after enabling keep-alive?
Usually the app closes idle connections sooner than Nginx expects. Make the app’s keep-alive timeout longer than Nginx’s.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.