Lesson 7 of 9 · 28m
Worker Models, Buffers, and Performance Tuning
Tune Nginx and Apache from their concurrency models outward — workers, keepalive, buffers, compression, and upstream capacity — instead of copying magic numbers.
Performance tuning advice usually fails because it treats the web server as a collection of mystical integers. It is not. It is a concurrency system. First understand how connections become work, then tune the bottleneck you actually observe. If you cannot say whether you are limited by CPU, sockets, upstream latency, disk, or memory, you are not tuning; you are cargo-culting.
Nginx is event-driven
worker_processes auto;
events {
worker_connections 4096;
multi_accept on;
}A small number of workers handles many connections asynchronously. Capacity is roughly constrained by worker count times connection count, but real limits appear earlier through file descriptors, TLS CPU, upstream saturation, or bandwidth. Check ulimit -n and systemd unit LimitNOFILE; the config can ask for connections the operating system will not permit.
Apache depends on the MPM
apache2ctl -V | grep -i mpm
apache2ctl -M | grep mpmevent is the modern threaded MPM for most reverse-proxy and static workloads. prefork survives because some embedded runtimes are not thread-safe, especially older PHP deployments. worker exists but is less common in new deployments. Tuning MaxRequestWorkers without knowing the MPM is meaningless because process and thread accounting differ.
Keepalive is a tradeoff, not a freebie
keepalive_timeout 65s;
keepalive_requests 1000;Long keepalive reduces handshake and TCP setup cost. It also holds connection state. In front of a large fleet, long idle lifetime can pin clients to draining infrastructure. Short lifetime increases TLS CPU and connection churn. Match behavior to your load balancer and CDN timeouts; mismatched idle timers produce intermittent reset or 502 noise.
Buffers shape memory under hostile or slow clients
client_body_buffer_size 128k;
client_max_body_size 20m;
proxy_buffering on;
proxy_buffers 16 16k;
proxy_busy_buffers_size 32k;Proxy buffering decouples fast upstreams from slow clients. Turning it off can help streaming responses but exposes upstream workers to client slowness. Large uploads belong on routes designed for them, not as a global invitation. Every buffer is memory multiplied by concurrency; arithmetic beats vibes.
Compression is CPU versus bandwidth
gzip on;
gzip_comp_level 4;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript image/svg+xml;Do not compress already compressed formats such as JPEG, PNG, MP4, or zip archives. At high traffic, consider precompressed static assets with gzip_static or a CDN. TLS plus compression can have security implications when secrets and attacker-controlled input share a response context; that is why indiscriminate compression fell out of favor in sensitive applications.
Measure the right bottleneck
ss -s
ss -ltnp
top -H
sar -n DEV 1
curl -w 'connect=%{time_connect} tls=%{time_appconnect} ttfb=%{time_starttransfer} total=%{time_total}\n' -o /dev/null -s https://app.sudosecurity.org/A slow total time with fast TTFB means transfer or buffering. Slow TTFB after fast TLS means upstream latency. High CPU on every core means concurrency increases will not help. Add observability before changing five knobs at once.
Tune in one dimension at a time
Change one setting, measure under realistic load, and record the result. The most dangerous performance changes are the ones that improve a synthetic benchmark while harming tail latency or memory stability. Production traffic contains slow phones, malicious clients, large uploads, API clients, health checks, and caches. Your benchmark should too.
Field notes
worker_processes autousually means one per CPU core; more is not automatically better for event-driven Nginx.- Apache process memory multiplied by max workers is a hard capacity ceiling. Count memory honestly before raising limits.
- TLS session reuse and HTTP/2 change the cost curve; benchmark your actual protocol mix.
- Do not tune buffers using a single localhost request. Loopback hides the slow-client behavior buffering exists to solve.
Was this lesson helpful?