Tinsh Tinsh Tuesday, November 17, 2026

When using Nginx's stream module to route HTTPS traffic based on Server Name Indication (SNI), unknown hostnames should be rejected as early as possible.

A common approach is to forward unmatched SNI requests to a default HTTPS server and use return 444 to close the connection. However, Nginx provides a more efficient solution: ssl_reject_handshake on.

This article explains the configuration and why it is a better fit for this scenario.

1. Architecture

The setup uses the Nginx stream module to inspect TLS ClientHello messages and route connections according to their SNI hostname.

stream {
    map $ssl_preread_server_name $stream_map {
        example.com      website;
        www.example.com  website;
        default          127.0.0.1:4443;
    }

    upstream website {
        server 127.0.0.1:8080;
  }

    server {
        listen 443;
        listen [::]:443;

        ssl_preread on;
        proxy_protocol on;
        proxy_pass $stream_map;
    }
}

The routing behavior is straightforward:

  • Known SNI names are forwarded to their corresponding HTTPS backends.

  • Unknown SNI names and connections without SNI are forwarded to the default backend.

The proxy_protocol on directive preserves the original client connection information for the backend.

2. The Original Approach: return 444

Initially, the default HTTPS server used return 444:

server {
    listen 127.0.0.1:4443 ssl proxy_protocol default_server;
    server_name _;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    return 444;
}

This works, but there is an unnecessary step.

Nginx must complete the TLS handshake, send its certificate, and wait for an HTTP request before executing return 444.

Since the purpose of this server is simply to reject unknown SNI connections, completing the TLS handshake serves no useful purpose.

3. A Better Approach: ssl_reject_handshake

Nginx provides ssl_reject_handshake, available since version 1.19.4.

The default server can be simplified to:

server {
    listen 127.0.0.1:4443 ssl proxy_protocol default_server;
    server_name _;

    ssl_reject_handshake on;
}

This changes the behavior significantly:

  • No TLS handshake is completed.

  • No server certificate is sent.

  • No HTTP request needs to be received.

  • The connection is rejected during the TLS handshake.

The certificate directives are no longer necessary in this default server.

Importantly, proxy_protocol must remain enabled because the stream proxy sends a PROXY protocol header before forwarding the TLS ClientHello.

4. Understanding the Difference

Behavior return 444 ssl_reject_handshake on
TLS handshake Completed Rejected
Server certificate Sent Not sent
HTTP request Required Not required
HTTP processing Performed Not reached
Default certificate Required Not required

Both approaches can reject unwanted connections, but ssl_reject_handshake operates at the appropriate protocol layer.

5. Testing the Configuration

After updating the configuration, check its syntax and reload Nginx:

nginx -t
systemctl reload nginx

Test an unknown SNI hostname:

curl -vk --resolve unknown.example:443:127.0.0.1 \
    https://unknown.example/

With ssl_reject_handshake on, the TLS handshake should fail before an HTTP request is sent.

This differs from the previous return 444 behavior, where TLS succeeds but curl reports an empty reply after the HTTP request.

6. Conclusion

When Nginx uses the stream module for SNI-based HTTPS routing, an unmatched hostname does not need a complete TLS session.

Using ssl_reject_handshake on in the default HTTPS server provides a cleaner and more efficient rejection mechanism than return 444.

It also eliminates unnecessary certificate configuration and keeps the responsibility for rejecting unknown TLS connections at the TLS layer.

The general principle: reject unwanted connections at the earliest appropriate protocol layer.

Only authorized users can post comments. Please register or login using one of these services: