DEV Community

johnettagaweraks
johnettagaweraks

Posted on

Reject unknown TLS server names before HTTP routing

Reject unknown TLS server names before HTTP routing

An HTTPS listener needs a default server block in nginx. That block answers when a client sends no matching server name or omits SNI entirely. If the default block serves a normal application, an unknown hostname can receive a certificate and an HTTP response that belong to a real service. The connection may look harmless in an access log, but it widens the service's discovery surface and can confuse certificate inventories.

Nginx supports ssl_reject_handshake on in a server block. Used on a dedicated default HTTPS listener, it rejects the TLS handshake before HTTP request routing begins.

A dedicated default listener

The configuration should make the default block explicit and keep the real virtual hosts in separate server blocks.

server {
    listen 443 ssl default_server;
    ssl_reject_handshake on;
}

server {
    listen 443 ssl;
    server_name app.example.net;
    ssl_certificate     /etc/nginx/tls/app.crt;
    ssl_certificate_key /etc/nginx/tls/app.key;

    location / {
        proxy_pass http://app_backend;
    }
}
Enter fullscreen mode Exit fullscreen mode

Nginx's HTTPS guide recommends limiting connections without SNI through ssl_reject_handshake in the default server. The directive is a routing control, not authentication. A client that presents the correct server name still reaches the real service and must satisfy the application's own authorization checks.

Compatibility comes first

Clients that do not send SNI will fail against this setup. That is the intended effect for an internet listener that publishes named services, but it may break old devices, monitor checks, or internal tools that connect by IP address. Review those clients before rollout.

Some environments intentionally serve a fallback page or a maintenance certificate. If so, write the policy down and make the fallback page contain no application data. Do not use a production virtual host as the fallback merely because its certificate is already available.

Check the complete hostname path

The default listener is only one part of hostname control. Review every server_name, wildcard, certificate, redirect, and upstream route. A wildcard may accept names the application does not own. A redirect from an unknown name can still disclose the expected host if it points to the canonical site.

Test with a matching SNI name, an unknown name, no SNI, an IP address, and a request whose Host header differs from SNI. Observe where each case terminates in the configuration. Keep TLS logs separate from application access logs so a rejected handshake can be distinguished from an HTTP 404.

For certificate inventory, record the vhost name, certificate subject, SANs, default-server status, and application owner. A rejected default handshake makes unknown-name traffic easier to classify and prevents the routing layer from presenting an unrelated application before policy has a chance to run.

References

Top comments (0)