Related posts
- nginx with OpenSSL 3.5, OpenSSL 4.1, AWS-LC or BoringSSL
The same nginx 1.31 built against four TLS libraries. AWS-LC and BoringSSL need half of OpenSSL's CPU for an ECDSA handshake, post-quantum key exchange included. Keep-alive and bulk transfers tie. OpenSSL 4.1 beats the 3.5 LTS and stays the default, because the others take features away.
- XenForo on PHP 8.1 to 8.6: the same build six times
Six PHP series compiled with the same clang recipe and the same extensions, one XenForo forum with 100,000 posts, plain nginx, PHP-FPM and MariaDB. PHP 8.5 and 8.6 serve uncached pages 8-11 % faster than 8.1. 8.2, 8.3 and 8.4 tie, and 8.6 adds nothing over 8.5 yet.
- Dropping the packages: compiled nginx, PHP and Valkey, and what the migration tests broke
My setup script now builds nginx, PHP and Valkey from source and installs none of them from packages. Live servers migrate once, on --optimize. Testing that migration and a PHP downgrade chain found eight bugs, including a build log that grew to 1.7 TB. A day later, 1.22.1 dropped pre-compressed static files.
With kernel TLS, nginx and OpenSSL do the handshake as usual and then hand the session keys to the kernel. From then on the kernel encrypts. That lets nginx use
sendfile()on HTTPS too, so file data never passes through user space. The usual claim is a big CPU saving on static files.Turning it on takes three things:
enable-ktls. My build tool now hasKTLS=1for that.ssl_conf_command Options KTLS;andsendfile on;in nginx.tlskernel module.I used the harness from the TLS-library post, with OpenSSL 3.5 and 4.1, each with and without kTLS.
First result: nothing worked#
The kTLS builds answered small requests, but a 1 MB file never finished. It stopped after a few hundred kilobytes, and wrk logged zero completed requests in 10 seconds. The error log was empty.
straceon a worker showed the pattern:The kernel took one 16 KB TLS record, then reported the socket full. nginx was only told it could write again by a timer about 200 ms later. That works out to 80 KB per second.
The cause turned out to be a sysctl my own setup script writes:
It keeps the unsent part of each socket’s buffer small, so HTTP/2 can still reprioritise streams. A socket counts as writable only while less than 16 KB is waiting to go out. One kTLS record from
sendfile()is 16 KB of data plus TLS overhead, so it is always just over the mark. And the queued record was apparently not pushed out right away, so nothing woke nginx until the timer fired.Changing the value settled it:
It was the same with OpenSSL 3.5 and 4.1 on kernel 7.0. Without kTLS, the low value does no harm, and raising it changed nothing for the non-kTLS builds either.
Second result: what kTLS buys#
All four builds, with the mark at 131072. Same host and method: nginx on 8 logical CPUs, wrk on 4, clock pinned, 5 rounds, medians. The column header says when kTLS was on.
OpenSSL 3.5 gave the same picture within a few percent.
Why it stays off#
My servers mostly send two kinds of responses:
sendfile()at all, so kTLS never sees them.On top of that, kTLS would need
tcp_notsent_lowatraised for the whole host, which gives up the HTTP/2 behaviour that setting is there for. On a forum or blog origin behind Cloudflare, that trade comes out negative.kTLS is worth it on a server that mainly sends large static files, such as downloads, video or image hosting, and that terminates TLS itself. For that case,
KTLS=1is in the build tool. The requirement is now written next to it and next to the sysctl line, and the benchmark refuses to run a kTLS build below 131072, so nobody measures a stall by mistake.One more thing broke along the way. A stalled worker survived its master being killed, and kept accepting connections on the next build’s ports through
reuseport. My first library comparison quietly mixed two builds before I noticed the kTLS counters ticking on a build that had no kTLS. The harness now kills the workers too, and refuses to start on a busy port.