kTLS — kernel TLS — moves the record-layer encryption and decryption of a TLS connection from userspace into the Linux kernel socket layer. Nginx 1.21.4 and later can enable it with ssl_conf_command Options KTLS;, and on a VPS with an AES-NI capable CPU this removes two copies of every record and lets the kernel hand pages directly to the NIC or the receiving socket. The gain is real but modest, and it is easy to measure wrongly by comparing the wrong TLS version or the wrong cipher.
This article sets up kTLS on Nginx, confirms the kernel path is actually taken, and benchmarks it against userspace OpenSSL with a method that isolates the TLS layer. If you have not yet sized the instance, the CPU and disk baselines in our VPS performance guides are the right starting point.
Check that the kernel supports kTLS
kTLS has been in mainline since 4.13 and the software crypto fallback since 5.1. Both TX and RX offload paths are registered against the cipher implementation, so the first thing to confirm is which ciphers the running kernel exposes.
grep TLS /boot/config-$(uname -r)
cat /proc/crypto | awk '/^name/ {n=$3} /^type/ {t=$3} /^driver/ {d=$3}
n ~ /tls/ && t == "skcipher" {print n, d}' | sort -u
openssl version -a | head -3
nginx -V 2>&1 | tr ' ' '\n' | grep -i ssl
You want to see tls12_aes_gcm_256, tls12_aes_gcm_128 and ideally aesni_intel as the driver. The AES-CCM variants exist but are rarely negotiated. ChaCha20-Poly1305 has a kTLS implementation too, but it only helps when the CPU lacks AES-NI; on any modern Xeon or EPYC, AES-GCM is faster and you should benchmark with that.
Enable kTLS in Nginx without breaking the config
Add the directive inside a server block that already has HTTP/2 or TLS 1.3 configured. Note that as of Nginx 1.25, kTLS is supported for TLS 1.2 and TLS 1.3 on Linux; the directive silently degrades if the kernel lacks the cipher, which is a useful failure mode but also means you must verify rather than assume.
server {
listen 443 ssl;
http2 on;
server_name example.test;
ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES256-GCM-SHA384;
ssl_conf_command Options KTLS;
location / { root /srv/www; }
}
Reload with nginx -t && systemctl reload nginx, then confirm from the outside that the negotiated cipher is AES-GCM. If the client falls back to ChaCha20 the kTLS TX path still works but the performance story changes, and you will have benchmarked a different configuration than you think.
Confirm kTLS is actually engaged
Two independent checks are worth doing. The first is ss, which reports TLS information on established sockets. The second is the kernel’s crypto accounting, which increments per-cipher request counters only when the kernel is doing the work.
ss -ti '( sport = :443 )' | head -20
# Look for "tls12_aes_gcm_256" in the socket info block
cat /proc/crypto | awk '/tls12_aes_gcm_256/{f=1} f&&/^[a-z]/ {print} /^$/{if(f)exit}'
# Per-CPU TLS counters (kernel 5.15+)
grep -i tls /proc/net/tls_stat 2>/dev/null || echo "tls_stat not present"
strace -f -e trace=setsockopt -p $(pgrep -o nginx) 2>&1 | grep -i ktls | head
The ss line is the most reliable: a socket showing tls12_aes_gcm_256 is a kernel-offloaded socket. If the socket shows only congestion-control detail and no cipher name, Nginx accepted the directive but the kernel path was not entered — usually because the cipher chosen by the client is not the one listed in /proc/crypto, or because the Nginx build lacks --with-openssl support for the option.
Benchmark the TLS layer in isolation
Do not benchmark with wrk against a dynamic page; you will measure PHP, not TLS. Serve a 1 MB static file and hammer it with keepalive connections so the handshake cost is amortised and the record path dominates.
head -c 1048576 /dev/urandom > /srv/www/1m.bin
wrk -t4 -c128 -d60s --latency \
--header 'Host: example.test' https://127.0.0.1/1m.bin
# CPU split: kernel vs userspace during the run
pidstat -p $(pgrep -o nginx) 1 30 | awk '{print $1, "usr="$4, "sys="$5}'
Run the same test with ssl_conf_command Options KTLS; commented out. The total request rate typically moves by 5–20% depending on payload size; the more interesting number is the sys versus usr split. With kTLS the userspace share drops sharply because OpenSSL stops doing the bulk work per record — the CPU time moves into the kernel sendfile path but total CPU per byte should fall.
Watch for the cases where kTLS does not help
Three conditions routinely erase the gain. First, small responses: if your median response is 2 KB, the per-record overhead dominates and the copy elimination is worth almost nothing. Second, TLS termination in front of dynamic app servers where the bottleneck is PHP-FPM or the database — the TLS layer may be under 5% of total CPU. Third, hosts where AES-NI is masked and you fall back to the generic AES implementation, in which case OpenSSL’s hand-tuned assembly may actually beat the kernel path.
Verify AES-NI is visible inside the guest with grep -o aes /proc/cpuinfo | head -1. If it is missing, stop and fix that first — it is worth an order of magnitude more than any TLS offload setting. When you are ready to size the plan around real TLS throughput rather than guesses, compare VPS plans with the benchmark numbers in hand.
A measurement checklist you can re-run
Keep one script that prints the negotiated cipher, the ss socket line, and the wrk result for both configurations. Run it after every Nginx upgrade, because the directive’s behaviour has changed more than once and a silent regression is easy to miss. Record the payload size alongside the request rate.
The honest conclusion is that kTLS is a free 5–15% on static-heavy TLS workloads and approximately nothing elsewhere. It costs one directive and one verification step, so it is worth enabling — but it is not a substitute for fixing application-side CPU or sizing the instance correctly, and the measurement discipline above is what keeps it from becoming cargo-cult configuration.




Leave a Reply
You must be logged in to post a comment.