TUNING FOR HIGH TRAFFIC

How to configure nginx, TLS, caching and kernel limits so one dedicated server absorbs real traffic on Ubuntu 24.04.

Tuning a dedicated server for high traffic

 

High-traffic web server setup: tuning a dedicated server

This is a high traffic web server setup you can follow top to bottom: nginx tuning on a dedicated server, done on Ubuntu 24.04 LTS. It covers TLS, caching, kernel limits and load testing, with copy-paste config and the reasoning behind each choice.

Server.Biz sells unmanaged dedicated servers, so you are the sysadmin here. Everything below is standard Linux web server optimization; nginx is the worked example, but the ideas carry to any origin you run.

Why high traffic lands on dedicated hardware

A high traffic web server setup asks for things a small virtual machine cannot give: dedicated CPU cores for TLS handshakes, memory bandwidth for cache locality, and an uplink that will not throttle under a media-heavy load. On dedicated hardware those resources are yours alone, so a traffic spike does not fight a noisy neighbour for the same cores.

Three costs dominate at scale. Every new HTTPS connection runs a TLS handshake, and handshakes are CPU work, so a burst of first-time visitors is a burst of asymmetric crypto. Concurrent connections each hold a file descriptor and a slice of memory, so connection count, not request rate alone, sets your ceiling. And sites that serve images, video or downloads are egress-heavy: the bottleneck becomes the uplink and the monthly traffic allowance, not the CPU.

A dedicated box gives you room on all three axes, plus a larger included traffic tier from the network as you move up the range. Exact allowances and prices live in the live catalog.

A CDN in front is complementary, not competing. Offloading static assets and media to a CDN cuts egress and shields the origin, while the dedicated server still does the dynamic work a CDN cannot cache. The tuning below makes that origin fast and hard to knock over.

Base hardening, then nginx from the official repo

Before any tuning, get the security basics right: a non-root admin user, SSH keys, a default-deny firewall, unattended security updates and a tested backup. Our guide on getting your server online covers that first hour, and you should do it before you expose a web server to the internet.

Everything here targets Ubuntu 24.04 LTS. Ubuntu ships an older nginx, so install from the official nginx.org repository to get current mainline builds, which is also where HTTP/3 support lives.

Add the nginx.org signing key and mainline repository, then install and start the service.

shell
sudo apt update
sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
  | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/ubuntu noble nginx" \
  | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install -y nginx
sudo systemctl enable --now nginx

Confirm the version and that the HTTP/3 (QUIC) module is compiled in before you rely on it.

shell
nginx -v
nginx -V 2>&1 | grep -o with-http_v3_module

TLS 1.3, session resumption and HTTP/3

Terminate TLS well and you cut the single biggest per-connection CPU cost. Get a certificate with certbot and the nginx plugin, which also writes the initial TLS config for you. If you prefer a shell-only client with easy wildcard and DNS-01 support, acme.sh is a good alternative; the server block below is the same either way.

Install certbot and issue a certificate. Replace YOUR_DOMAIN with your own hostname.

shell
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d YOUR_DOMAIN -d www.YOUR_DOMAIN

The TLS and protocol settings that matter, in a server block. Enable HTTP/2 and HTTP/3 side by side so every client gets the best it supports.

/etc/nginx/conf.d/YOUR_DOMAIN.conf
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    listen 443 quic reuseport;
    listen [::]:443 quic reuseport;
    http2 on;

    server_name YOUR_DOMAIN www.YOUR_DOMAIN;

    ssl_certificate     /etc/letsencrypt/live/YOUR_DOMAIN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/YOUR_DOMAIN/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;

    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    ssl_stapling on;
    ssl_stapling_verify on;

    add_header Alt-Svc 'h3=":443"; ma=86400' always;

    root /var/www/YOUR_DOMAIN;
    index index.html;
}

TLS 1.3 needs one round trip fewer than 1.2 to set up a connection, so keep both enabled and let clients pick 1.3. Session resumption via the shared cache lets returning clients skip the full handshake entirely, which is the cheapest win on this list.

OCSP stapling lets the server prove the certificate is not revoked, so clients do not stall on a separate lookup. The reuseport flag on the QUIC listeners lets the kernel spread new connections across worker processes, which matters on many-core machines; set it on only one server block per address. The Alt-Svc header advertises QUIC, so HTTP/3-capable browsers upgrade themselves on the next request.

The nginx.conf settings that carry load

The main config decides how many connections a machine will hold and how cheaply it serves repeat files. These are the directives that move the needle under load; leave the rest at their defaults. Comments are stripped here so you can paste it clean, with each choice explained below.

The parts of nginx.conf worth changing for a busy site.

/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 16384;
    multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay   on;

    keepalive_timeout  65;
    keepalive_requests 1000;

    open_file_cache          max=100000 inactive=30s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;

    gzip            on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_vary       on;
    gzip_types      text/plain text/css application/json application/javascript application/xml image/svg+xml;
    gzip_static     on;

    include /etc/nginx/conf.d/*.conf;
}

gzip vs brotli, honestly

gzip is built into nginx; brotli is not part of the stock nginx.org package. You have two honest options: build the ngx_brotli module, or precompress your assets to .gz (and .br) at deploy time and let gzip_static serve them without compressing on the fly. Precompressed assets give the best ratio at zero request-time CPU, so for most sites that is the better trade.

worker_processes auto runs one worker per CPU core, and worker_connections is per worker, so an 8-core box at 16384 can in theory hold well over 100,000 concurrent connections, long before which some other limit binds. worker_rlimit_nofile raises the file-descriptor ceiling each worker is allowed.

sendfile with tcp_nopush lets the kernel send file data and headers in full packets, which is efficient for static files, while keepalive_timeout and keepalive_requests keep connections open so repeat requests skip TCP and TLS setup. open_file_cache keeps file handles and stat results in memory, so a hot static file is not re-opened on every hit.

Caching layers, from static assets to a microcache

The cheapest request is the one nginx answers from cache without touching your application. Layer it: a long, immutable cache on static assets, and a short-lived cache in front of dynamic pages.

Give fingerprinted static assets a long, immutable cache in your server block.

/etc/nginx/conf.d/YOUR_DOMAIN.conf
location ~* \.(?:css|js|woff2|jpg|jpeg|png|webp|avif|svg|ico)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
    access_log off;
}

Define a one-second microcache zone in the http block of nginx.conf.

/etc/nginx/nginx.conf
fastcgi_cache_path /var/cache/nginx/micro levels=1:2
                   keys_zone=MICRO:20m inactive=1m max_size=1g;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

Then use it in the PHP location, skipping the cache for logged-in users and non-GET requests.

/etc/nginx/conf.d/YOUR_DOMAIN.conf
map $request_method $micro_bypass {
    default 0;
    POST    1;
}

map $http_cookie $micro_cookie_bypass {
    default                    0;
    "~*(sessionid|logged_in)"  1;
}

server {
    # ... TLS block from above ...

    set $skip_cache 0;
    if ($micro_bypass)        { set $skip_cache 1; }
    if ($micro_cookie_bypass) { set $skip_cache 1; }

    location ~ \.php$ {
        include        fastcgi_params;
        fastcgi_pass   unix:/run/php/php8.3-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_cache           MICRO;
        fastcgi_cache_valid     200 1s;
        fastcgi_cache_bypass    $skip_cache;
        fastcgi_no_cache        $skip_cache;
        fastcgi_cache_lock      on;
        fastcgi_cache_use_stale updating error timeout;
        add_header X-Cache      $upstream_cache_status;
    }
}

One second sounds too short to matter. It is not. Under a spike, a page requested 5,000 times a second is generated once and served from memory 4,999 times, and fastcgi_cache_lock means only one request rebuilds the page while the rest wait for that single result. That is how a microcache absorbs a thundering herd without serving anything a visitor would notice as stale.

The cache key decides what counts as the same page: scheme, method, host and URI here. The bypass variables are your escape hatch, so logged-in sessions and form posts always reach the application. Watch the X-Cache header while testing to confirm HIT and BYPASS behave as you expect. For a reverse-proxied app (Node, Go, Python), the same pattern uses proxy_cache_path and proxy_cache instead of the fastcgi_ variants.

Kernel and OS limits for many connections

nginx can only open as many connections as the OS allows. Two limits bind first under load: the per-process file-descriptor cap, and the kernel connection backlog. Raise them deliberately, not by copying a giant block from a blog.

Raise the descriptor limit for the nginx service. worker_rlimit_nofile in nginx.conf cannot exceed this.

/etc/systemd/system/nginx.service.d/override.conf
[Service]
LimitNOFILE=131072

Reload systemd and restart nginx to apply the new limit.

shell
sudo systemctl daemon-reload
sudo systemctl restart nginx

A small sysctl file, every line with a reason. Do not paste 40-line blocks from random blogs; most of it is cargo cult and some of it hurts.

/etc/sysctl.d/99-webserver.conf
# Queue depth for accepted-but-not-yet-handled connections.
# Must be >= the listen backlog nginx asks for.
net.core.somaxconn = 65535

# More ephemeral ports for outbound connections to PHP-FPM or upstreams.
net.ipv4.ip_local_port_range = 1024 65535

# Bigger SYN queue so connection bursts are not dropped.
net.ipv4.tcp_max_syn_backlog = 8192

# Packets the NIC can queue before the kernel processes them.
net.core.netdev_max_backlog = 16384

# System-wide file-descriptor ceiling, above the per-service limit.
fs.file-max = 1000000

Load the file and confirm one value took effect.

shell
sudo sysctl --system
sysctl net.core.somaxconn

somaxconn only helps if nginx actually asks for the deeper backlog, so match it on the listen directive, for example listen 443 ssl backlog=65535 on your main server. Every other line above has a one-line justification. If you cannot say what a sysctl line does, do not set it.

Sizing PHP-FPM, or proxying an app

For PHP sites the application, not nginx, is usually the ceiling. FPM runs a pool of worker processes, and the one number that matters is how many workers fit in RAM.

Ubuntu 24.04 ships PHP 8.3. Install FPM and the extensions your app needs.

shell
sudo apt install -y php8.3-fpm php8.3-mysql php8.3-xml php8.3-mbstring

Set the process manager and worker count in the pool config.

/etc/php/8.3/fpm/pool.d/www.conf
pm = static
pm.max_children = 40
pm.max_requests = 500

Size pm.max_children from memory, not guesswork. Measure a real worker peak resident size (60 to 120 MB is typical for a framework app), reserve RAM for the OS, nginx and any database on the box, then divide what remains by that per-worker figure. On a 4 GB machine dedicated to web serving, reserving about 1.5 GB and assuming 80 MB per worker gives roughly (4096 - 1536) / 80, so about 30 workers. Round down; a machine that swaps under load is slower than one that queues.

pm = static preallocates every worker at startup: no fork latency during a spike and predictable memory, which is right when PHP owns the box. Use pm = dynamic instead when the server shares RAM with other services, so idle workers are released. pm.max_requests recycles a worker after N requests, which caps the damage from a slow memory leak.

Proxying an app server (Node, Go, Python) follows the same logic: run enough app processes to use your cores, put nginx in front with proxy_pass and the microcache above, and let nginx handle TLS, keepalive and static files so the app only does dynamic work.

Load test before launch, and read p99

Never let real traffic be your first load test. Drive the server with wrk or hey from a second machine and watch where it bends.

hey sends a fixed load and prints a latency histogram. 200 concurrent clients for 30 seconds:

shell
hey -z 30s -c 200 https://YOUR_DOMAIN/

wrk pushes higher concurrency with fewer threads and reports tail latency with the --latency flag.

shell
wrk -t8 -c400 -d30s --latency https://YOUR_DOMAIN/

Read the p99, not the average. An average of 40 ms hides the reality that 1 in 100 users waited 900 ms, and those are the requests that pile up and tip a server over. Raise concurrency step by step, 100, 200, 400, 800, and watch for the knee: the point where throughput stops rising but p99 latency climbs fast. That knee is your real capacity, and where you decide whether tuning or more hardware is the answer.

Two rules. Test from another machine, not the server itself, or the load generator steals the CPU you are trying to measure. And test your own application layer, not the network path in front of it. The always-on DDoS filtering is built to notice a flood from a single source, so a synthetic hammer aimed at your public IP tells you more about the filter than about your app. Keep the volume sane, and if you must run something large, tell the datacenter partner first.

When one box is not enough

Tune first, then scale, and scale in the cheapest way that works. The order matters.

Scale up before you scale out. A bigger single machine, more cores, more RAM, faster NVMe, is far simpler than a cluster: one config, one set of logs, no shared-state problem. Most sites that think they need three servers actually need one properly sized server with the caching above. Buy headroom before you buy complexity.

When one box genuinely runs out, split roles before you split load. Move the database to its own dedicated machine so it stops competing with nginx and PHP for memory and disk. A database server with its own NVMe and RAM is the highest-value split and a natural first step, and it often buys another large multiple of headroom on its own.

Only then run multiple web nodes behind a load balancer, with the database and a shared cache on their own machines and sessions kept out of local memory. That is a real architecture with real operational cost, so reach it deliberately, not by reflex. The bottleneck table below tells you which limit you are actually hitting, so you scale the right thing.

Common bottlenecks and the fix

Bottleneck What you see The fix
TLS handshake CPU CPU spikes when new visitors arrive TLS 1.3, session resumption and keepalive; terminate on all cores
File descriptors "Too many open files" in the error log Raise worker_rlimit_nofile, systemd LimitNOFILE and fs.file-max
PHP workers 502 or 504 and a request queue under load Size pm.max_children to RAM; add the microcache
Disk IO High iowait and slow first byte on files NVMe storage, open_file_cache, keep hot data in RAM
Bandwidth Uplink saturates on media and downloads Move to a higher traffic tier; put a CDN in front of media

The DDoS layer sits upstream of all of this

Everything above makes your origin fast. It does not stop a volumetric attack, and it should not try to: that is the network job. The network in front of every server includes always-on filtering that scrubs attack traffic before it reaches your uplink. Tune the app, and let the layer built for floods handle floods. Our guide on DDoS protection explains how that layer works.

Continue reading

Need help choosing?

Our experts can help you find the perfect server for your needs.

Contact us »