Where does TLS handshake cost come from at an Nginx edge?

A full TLS handshake costs time on the network and CPU on the server. In TLS 1.2, the client waits two round trips before sending application data. The server sends its certificate chain and signs the key exchange with its private key, which is expensive with RSA keys. The client then validates the chain. A resumed handshake reuses previously negotiated secrets. The server skips sending the certificate and skips the signature, and in TLS 1.2 resumption saves a full round trip.

The second cost sits on the client. A client that checks revocation may contact the CA's OCSP responder, which is a separate host with its own DNS lookup, connection and latency. That delay is outside your control unless the server supplies the answer itself.

Aspect Full handshake (TLS 1.2) Resumed handshake (TLS 1.2)
Round trips before data 2 1
Certificate chain sent Yes No
Server private-key signature Yes No
Client chain validation / revocation check Yes No

Illustrative estimate (assumed numbers, not a benchmark).

  • Assume a 100 ms RTT and a resumption rate r.
  • Average handshake latency per new connection = RTT × (2 − r).
  • At r = 0 this is 200 ms. At r = 0.8 it is 120 ms.
  • A client opening 10 new connections per session therefore waits about 800 ms less in total.

High-churn traffic benefits most:

  • Short-lived API calls without keep-alive
  • Mobile clients whose connections drop often
  • Many distinct clients reconnecting

Long-lived keep-alive or HTTP/2 connections pay the handshake once and then reuse it, so tuning resumption changes little for them.

 FULL HANDSHAKE (TLS 1.2): 2 RTT before application data
 +-----------+                              +--------------------+
 |  Client   |---- ClientHello ------------>|    Nginx edge      |
 |           |<--- cert chain + signed KX --| private-key sign   |
 |           |---- key exchange, Finished ->| (CPU cost, RSA)    |
 |           |<--- Finished ----------------|                    |
 +-----+-----+                              +--------------------+
       |
       |  client validates chain, may check revocation
       v
 +-----------------+
 | CA OCSP         |  separate host: own DNS lookup,
 | responder       |  connection and RTT (client-side cost)
 +-----------------+

 RESUMED HANDSHAKE (TLS 1.2): 1 RTT before application data
 +-----------+                              +--------------------+
 |  Client   |---- ClientHello + session -->|    Nginx edge      |
 |           |<--- Finished (no cert) ------| reuse secrets,     |
 |           |---- Finished + app data ---->| no signature       |
 +-----------+                              +--------------------+
   no certificate sent, no chain validation, no revocation lookup

A full TLS 1.2 handshake costs two round trips, a server private-key signature, and a possible client trip to the CA's OCSP responder. A resumed handshake reuses earlier secrets and needs one round trip, with no certificate, signature, or revocation lookup.

How does it work: what Nginx does with each new TLS connection

Nginx hands the handshake to the TLS library it is linked against and controls that library's behavior through the ssl_* directives. For each new connection the decision path looks like this:

  1. The client offers something to resume, or nothing. In TLS 1.2 the ClientHello can carry a session ID, a session ticket, or neither. In TLS 1.3 it can carry a pre-shared key identity derived from a ticket received earlier.
  2. Nginx tries to recover the session. A session ID is looked up in the ssl_session_cache. A ticket is decrypted with one of the keys configured through ssl_session_ticket_key, or with the in-memory keys Nginx generated at startup if none are configured. The recovered session must still be within ssl_session_timeout.
  3. On success, the handshake is abbreviated. No certificate is sent and no private-key signature is computed. The client also does no chain validation or revocation check.
  4. On failure, a full handshake runs. The cause can be a cache miss, an expired session, or a ticket encrypted under a key this node does not have. The client never sees an error. It simply gets a full handshake and a fresh session ID or ticket for next time.
  5. Stapling applies only to full handshakes. When the client sends the status_request extension and Nginx holds a valid cached OCSP response for the certificate, Nginx includes that response with the certificate. Resumed handshakes send no certificate, so they carry no staple and need none.

The two features cover different halves of the cost. Resumption removes handshakes. Stapling makes the remaining full handshakes cheaper for the client. Each fails silently by falling back to the slower path. That is why the later sections stress measurement.

How do ssl_session_cache and session tickets work in Nginx?

Nginx offers two ways to skip the full handshake, and they put the session state in different places.

Server-side cache: ssl_session_cache

With a session cache, Nginx stores the negotiated session parameters in memory and gives the client an ID it can present on reconnect. The builtin cache is private to each worker process. A client that reconnects and lands on a different worker misses the cache, and the Nginx documentation warns that the builtin cache can fragment memory. shared:NAME:size places one cache in shared memory that all workers can read, so a returning client gets a hit no matter which worker accepts the connection.

Size the cache from the number of distinct sessions created within one ssl_session_timeout window. The documentation gives about 4,000 sessions per megabyte. As an illustrative estimate, 200,000 new sessions in a 10-minute window needs roughly 50 MB. When the cache fills, Nginx evicts the oldest entries.

The cache belongs to one host, not the fleet. If your load balancer spreads reconnects across nodes without stickiness, most resumption attempts reach a node that has never seen the session. A larger cache does not raise the hit rate in that case.

Stateless tickets: ssl_session_tickets

With tickets, the server encrypts the session state under a ticket key and the client stores the result. Any node that holds the same key can resume the session, and the server keeps no per-session memory. The tradeoff is key management. Anyone who obtains a ticket key can decrypt the recorded traffic of every session issued under it.

Timeout tradeoffs

A long ssl_session_timeout keeps cache entries alive longer, which costs more memory. It also extends how long a session's secrets can be reused, so one compromise exposes a wider window of traffic. That weakens forward secrecy.

Aspect ssl_session_cache (shared) ssl_session_tickets
State location Server memory Client, encrypted by server key
Multi-node behavior Per host; needs sticky routing Works fleet-wide if keys are shared
Memory cost About 1 MB per ~4,000 sessions Negligible on server
Key management burden None Distribute and rotate keys
Forward secrecy impact Bounded by timeout and eviction Bounded by key rotation interval
ssl_session_cache   shared:SSL:50m;

ssl_session_timeout 10m;

ssl_session_tickets on;

ssl_session_ticket_key /etc/nginx/tls/ticket.current.key;
ssl_session_ticket_key /etc/nginx/tls/ticket.previous.key;

An Nginx configuration that sizes a shared session cache, sets a moderate session timeout, and enables session tickets with a current and a previous ticket key so the fleet can rotate keys.

How does TLS 1.3 change session resumption?

TLS 1.3 drops the session ID mechanism. Resumption now uses a pre-shared key (PSK). After the handshake completes, the server sends one or more NewSessionTicket messages, and the client presents one of those tickets on its next connection. Every TLS 1.3 resumption therefore starts from a ticket. The ticket can either be self-contained and encrypted with the server's ticket key, or act as a handle to an entry in the server-side cache.

This matters for teams that set ssl_session_tickets off on the assumption that ssl_session_cache alone handles resumption. Under TLS 1.2 that assumption holds. Under TLS 1.3 the outcome depends on the TLS library Nginx is linked against:

  • Some libraries respond by issuing stateful tickets that point into the shared cache.
  • Others stop offering TLS 1.3 resumption entirely.

Check your own build with a resumption test before relying on either behavior.

0-RTT early data

With ssl_early_data on, a resuming client can send its first request inside the opening flight. This removes one round trip from time-to-first-byte. The trade-off is that early data has no replay protection at the TLS layer. An attacker who captures that flight can resend it, and the server will accept it again.

Enable early data only if your application either rejects replayed non-idempotent requests or can safely absorb them. To let upstreams make that decision, pass $ssl_early_data in a request header. The variable is set to 1 when a request arrived as early data, so the backend can refuse unsafe methods, for example by responding with 425 Too Early.

Most deployments should leave early data off. The latency saved is a single round trip, and getting replay handling wrong puts correctness at risk.

http {
    # Shared cache plus tickets. Under TLS 1.3 every resumption starts from a ticket,
    # so check resumption against your own build before turning tickets off.
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 1h;
    ssl_session_tickets on;

    # Flag non-idempotent requests that arrived as 0-RTT early data.
    map "$ssl_early_data:$request_method" $reject_early {
        default                          0;
        "~^1:(POST|PUT|PATCH|DELETE)$"   1;
    }

    upstream app_backend {
        server 127.0.0.1:8080;
    }

    server {
        listen 443 ssl;
        server_name example.com;

        ssl_certificate     /etc/nginx/tls/example.com.crt;
        ssl_certificate_key /etc/nginx/tls/example.com.key;
        ssl_protocols       TLSv1.2 TLSv1.3;

        # Off by default. Enable only if replayed requests are rejected or harmless.
        ssl_early_data on;

        location / {
            # Edge guard: refuse unsafe methods sent as early data.
            # The client can retry after the handshake completes.
            if ($reject_early) {
                return 425;
            }

            # Signal to the backend: "1" if the request came in as early data, empty otherwise.
            proxy_set_header Early-Data $ssl_early_data;
            proxy_pass
        }
    }
}

An Nginx configuration that keeps TLS 1.3 session tickets enabled, turns on 0-RTT early data, returns 425 Too Early for non-idempotent methods received as early data, and passes $ssl_early_data upstream in an Early-Data header so the backend can make its own replay decision.

How should you manage session ticket keys across multiple nodes?

By default, Nginx generates session ticket keys in memory when it loads its configuration. Every reload or restart produces new keys, and each node behind a load balancer has its own. A client that resumes against a different node, or against the same node after a deploy, presents a ticket that no server can decrypt. It falls back to a full handshake. Nothing errors, and nothing appears in the logs. The resumption rate just drops.

The fix is to supply keys explicitly with ssl_session_ticket_key, pointing at key files that are identical on every node. You can list the directive more than once. The first key encrypts new tickets. The remaining keys are only used to decrypt tickets issued before the last rotation. A typical rotation adds a fresh key at the top, moves the current key down, drops the oldest, and reloads. Distribute the files through your secret management system instead of baking them into images, and restrict their permissions as tightly as you would a private key.

The trade-offs:

  • Rotation interval. A key that rarely rotates lets an attacker who steals it decrypt recorded resumed sessions, which undermines forward secrecy. Rotating more often limits that exposure but invalidates outstanding tickets sooner, which lowers the resumption rate.
  • Blast radius. A shared key means a leak from one node exposes resumed traffic across the whole fleet.
  • Operational cost. You need a scheduled job, coordinated distribution, and a reload on every node. If rotation drifts out of sync, you get a silent resumption drop rather than an outage. That makes resumption rate a metric you need to alert on.
http {
    ssl_session_tickets on;

    # How long a resumed session stays valid. Keep it shorter than the
    # rotation interval times the number of listed keys, or tickets will
    # outlive the keys that can decrypt them.
    ssl_session_timeout 4h;

    # The FIRST key encrypts new tickets.
    ssl_session_ticket_key /etc/nginx/tls/ticket.key.curr;

    # The remaining keys are only used to decrypt tickets.
    # next: tickets from a node that has already rotated.
    # prev: tickets issued before the last rotation.
    ssl_session_ticket_key /etc/nginx/tls/ticket.key.next;
    ssl_session_ticket_key /etc/nginx/tls/ticket.key.prev;

    server {
        listen 443 ssl;
        server_name www.example.com;
        ssl_certificate     /etc/nginx/tls/www.example.com.crt;
        ssl_certificate_key /etc/nginx/tls/www.example.com.key;
    }
}

An nginx config that points every node at the same ticket key files: the first key encrypts new tickets, the others keep tickets from just before and just after a rotation decryptable, and the comments describe a rotation order that avoids resumption drops while nodes reload.

How does OCSP stapling work and how do you configure it in Nginx?

Without stapling, a client that wants revocation status for your certificate has to contact the CA's OCSP responder itself. That adds a lookup to the client's connection setup and tells the CA which sites the client visits. Stapling moves the lookup to the server. Nginx fetches a signed OCSP response, caches it, and sends it inside the handshake. The client verifies the CA's signature on the response and does not need to contact the responder.

Enable it with ssl_stapling on and ssl_stapling_verify on. Verification requires ssl_trusted_certificate to point at the issuer chain, so Nginx can check the response signature before serving it. Nginx also needs a resolver directive, because it resolves the responder hostname at runtime.

The fetch is lazy. Nginx requests a response only when a handshake needs one, so the first connections after a start or reload may get no staple.

ssl_stapling_file serves a response that was fetched outside Nginx. This avoids the lazy fetch, but your external job now owns freshness. If that job stops running, Nginx keeps serving an expired response, and clients will treat an expired response as worse than no staple at all.

Most stapling failures are silent. A responder outage, a broken resolver, or a missing issuer certificate only produce error log entries, and handshakes continue without a staple. That changes if your certificate carries the Must-Staple extension. Clients that honor it reject handshakes that arrive without a valid staple, so a stapling failure becomes an outage. With Must-Staple, stapling is an availability dependency and needs monitoring like one.

When should you avoid or skip OCSP stapling and resumption tuning?

Before relying on stapling at all, check whether your certificate still includes an OCSP responder URL. Some public CAs have reduced or dropped OCSP in favor of CRLs, so the Authority Information Access extension may list only a CA issuer URL. Running openssl x509 -noout -ocsp_uri -in cert.pem prints nothing in that case. Nginx does not refuse to start when the responder is missing. It logs a stapling warning and serves handshakes without a staple. A config review can therefore show ssl_stapling on while nothing is actually stapled.

Resumption tuning matters only when clients open many new connections. HTTP/2 multiplexes requests over one connection, and long keep-alive timeouts let HTTP/1.1 clients reuse sockets. Once one full handshake is spread across hundreds of requests, a bigger session cache saves little. The same applies to internal service-to-service traffic with persistent connection pools.

Measure your resumption rate and requests per connection before spending engineering time. The traffic shape tells you where the handshakes actually are.

Traffic / deployment shape Recommended action
Browsers on HTTP/2, long keep-alive Leave defaults; tune keepalive_timeout before resumption
Many short-lived API or mobile clients Tune session cache and shared ticket keys
Multiple nodes, no shared ticket keys Use sticky load balancing so cache-based resumption hits the same node
Certificate has no OCSP URL Disable stapling to remove log noise; revocation relies on CRLs
TLS terminated at an upstream load balancer or CDN Tune at that layer; Nginx-side settings have no effect

What are the alternatives to tuning resumption and stapling?

Session resumption and stapling are not the only ways to reduce handshake cost. Several alternatives work at a different layer. They often complement this tuning rather than replace it.

  • Fewer new connections. Raising keepalive_timeout, serving HTTP/2, and using persistent connection pools for upstream and service-to-service traffic all reduce how many handshakes happen at all. For traffic dominated by long-lived connections, this does more than any cache setting.
  • Cheaper full handshakes. An ECDSA certificate makes the server's signature much cheaper to compute than an RSA one. Nginx can serve an ECDSA and an RSA certificate side by side by listing ssl_certificate and ssl_certificate_key twice, so older clients that lack ECDSA support still connect. This cuts server CPU per full handshake but does not remove any round trips.
  • Sticky load balancing instead of shared ticket keys. Routing a client back to the same node lets a per-node ssl_session_cache resume sessions without distributing any key material. The trade-off is uneven load and lost sessions whenever a node is drained or restarted.
  • TLS termination elsewhere. A dedicated load balancer or CDN in front of Nginx can own the handshake. Resumption, ticket keys and stapling then become that layer's concern, and Nginx-side tuning stops mattering for client traffic.
  • HTTP/3 over QUIC. QUIC combines the transport and TLS 1.3 handshakes, so a new connection needs fewer round trips than TCP plus TLS. It still relies on TLS 1.3 tickets for resumption and 0-RTT, so the ticket key and replay concerns above carry over. It also adds UDP to your operational surface.
  • Alternatives to OCSP for revocation. Where a CA has moved to CRLs, there is nothing to staple and the client handles revocation itself. Short-lived certificates shrink the window in which revocation matters at all. They shift the operational burden to reliable, automated renewal.

How do you verify stapling and measure resumption rate in production?

To confirm a staple, run openssl s_client -connect host:443 -servername host -status and check for OCSP Response Status: successful in the output. Run it twice. Nginx fetches the OCSP response lazily, so the first connection after a reload often has no staple.

To test resumption, save a session with -sess_out sess.pem, then reconnect with -sess_in sess.pem and look for Reused rather than New in the output. Be careful with -reconnect under TLS 1.3. The server sends tickets after the handshake, so the client can reconnect before it has received one and report New even when resumption works.

Synthetic tests only show that resumption is possible. To see how often it actually happens, add $ssl_session_reused (r or .) and $ssl_protocol to a log_format, then aggregate per node. A low reuse rate on one node often points to a missing ticket key or a cache that isn't shared. Also alert on OCSP-related lines in the error log, because stapling fails silently from the client's side.

Placing directives

When many virtual hosts share a cache, set these at http level:

  • ssl_session_cache shared:SSL:…, using the same zone name everywhere
  • ssl_session_timeout
  • ssl_session_ticket_key
  • resolver

Keep these per server, since each host has its own chain:

  • ssl_certificate and its key
  • ssl_trusted_certificate
  • ssl_stapling and ssl_stapling_verify

Record the reuse rate and handshake latency before each change and compare afterward. Results depend heavily on client mix, so a measured delta is more useful than an assumed gain. It also tells you when to stop tuning.

How can this save engineering time?

The direct payoff of this work is latency and CPU. The engineering-time payoff comes from turning silent, intermittent problems into visible, bounded ones.

  • Shorter latency investigations. Both features fail silently by falling back to the slow path. Logging $ssl_session_reused and $ssl_protocol per node turns "some clients are slow after deploys" into a specific number on a specific node. Without that metric, engineers end up chasing the problem through packet captures and client-side traces.
  • Configuration written once, not per host. Putting the session cache, timeout, ticket keys and resolver at http level gives every virtual host the same behavior. New sites inherit it, and nobody has to rediscover the settings for each server block.
  • Key rotation as a job instead of an incident. A scripted rotation with staged next, current and previous keys removes manual coordination across nodes. It also prevents the post-deploy resumption drops that otherwise get investigated one at a time.
  • Fewer surprise outages from Must-Staple. Alerting on OCSP errors in the log, and confirming that the certificate still has an OCSP URL, catches stapling failures before clients that enforce Must-Staple start rejecting connections.
  • Avoiding work that would not pay off. Measuring requests per connection first tells you whether resumption tuning matters at all. For HTTP/2 browser traffic with long keep-alive, the answer is often no, and the time goes elsewhere.

Illustrative estimate (assumed inputs, not a measured result). Assume a fleet with no shared ticket keys deploys twice a week. Assume each deploy triggers one ticket from monitoring or a client team about elevated handshake latency, and each ticket takes an engineer about two hours to trace back to regenerated in-memory keys. That is roughly four hours a week. A one-time effort to set up shared keys, scripted rotation and a reuse-rate alert would remove most of it. Substitute your own deploy frequency and investigation time before deciding whether the setup is worth it.

How OneEnv can help

Disclosure: OneEnv is the product behind this blog.

OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.

Applied to nginx tls performance, this is where it helps:

Saves time

  • Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
  • One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
  • Changing a shared variable is one edit and one review instead of editing every service's .env by hand.

Saves cost

  • Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
  • Less duplicated configuration to maintain: one shared definition inherited by many services.
  • A wrong or stale value is caught at review time by the services it affects, not by a failed deploy.
  • OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.

These are qualitative benefits, not measured results — how much you save depends on how many services share the value.

Want to try it? Start at oneenv.io.