Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal-proxy

Lightweight proxy server for Kamal

View on GitHub

15 documents · Updated Nov 18, 2026

docs / tls-and-certificates

TLS and on-demand certificates

TLS protects the proxy listeners and lets each served domain use the right certificate. A service can use a certificate from disk or let ACME obtain certificates automatically. For domains that are not known in advance, on-demand TLS asks an approval endpoint before it obtains a certificate. This page complements Proxy server architecture, Request routing and middleware, and Proxy error responses.

Certificate configuration

TLS is a service setting. The setting controls whether the service accepts HTTPS traffic and which certificate source the proxy uses for that service.

ConfigurationPurpose
--tlsEnables TLS for the service.
--tls-certificate-path and --tls-private-key-pathUse a PEM certificate and its matching private key from disk.
ACME settingsObtain and renew certificates automatically. The ACME cache stores the resulting assets.
--tls-on-demand-urlApprove a domain at certificate request time instead of using a fixed host list.
--tls-redirectRedirect plain HTTP requests to HTTPS. The deploy command enables this by default.

Automatic TLS uses the configured ACME directory and cache path. The cache is scoped to the ACME directory, so a change of certificate authority settings does not reuse certificates from the old setting. Automatic TLS does not issue certificates for wildcard hosts. Use a static certificate when a wildcard certificate is required.

TLS settings belong on the service that serves the root path. Services below a root path receive the root service's TLS and redirect behavior. This keeps one host's scheme policy consistent across its path routes.

Certificate sources

The proxy selects one certificate source for each TLS-enabled service. A static source always returns the configured certificate. An automatic source uses ACME and restricts issuance to configured hosts unless on-demand approval is enabled.

References: internal/…/service.go• L101-118 internal/…/service.go• L129-168 internal/…/service.go• L542-584 internal/…/cert.go• L17-40 internal/…/deploy.go• L35-42

Listener handshake

The proxy uses the server name from the TLS handshake to find the service for a domain. The selected service then supplies its certificate manager, which either returns the static certificate or obtains the matching ACME certificate.

The HTTPS listener supports HTTP/1.1 and HTTP/2. When HTTP/3 is enabled, its TLS certificate lookup uses the same router path. If a client does not send a server name, the proxy can use a configured default TLS host. An unknown host or a service without TLS cannot supply a certificate.

This diagram shows how a client handshake reaches the service certificate source before the request enters normal proxy routing.

flowchart LR
    Client[TLS client] --> Listener[HTTPS or HTTP/3 listener]
    Listener --> Router[Service lookup by server name]
    Router --> Manager[Service certificate manager]
    Manager --> Static[Static certificate]
    Manager --> ACME[ACME certificate flow]
    ACME --> Cache[Certificate cache]

Server name selection

The router uses the handshake server name as the domain key. It resolves that name to the service that owns the host, then rejects the handshake when no matching TLS service exists.

References: internal/…/router.go• L325-349 internal/…/server.go• L151-176 internal/…/server.go• L249-255 internal/…/service_map.go• L81-96

On-demand authorization

On-demand TLS means the proxy does not need a complete host list before it can serve domains. When ACME needs a certificate for a new domain, the proxy asks the configured on-demand endpoint whether that domain is allowed.

The authorization result is simple: an HTTP 200 OK allows certificate issuance. Any other response or a failed check denies the request. The proxy adds the requested domain as the host query parameter and keeps any query parameters already present in the configured URL.

This diagram shows the request path for a domain that has no cached certificate.

sequenceDiagram
    participant Client
    participant Proxy
    participant Check as Approval endpoint
    participant CA as ACME server
    participant Cache

    Client->>Proxy: TLS handshake with server name
    Proxy->>Cache: Look for domain certificate
    alt Certificate exists
        Cache-->>Proxy: Return cached certificate
    else Certificate is absent
        Proxy->>Check: GET endpoint?host=domain
        Check-->>Proxy: 200 or denial
        alt Approved
            Proxy->>CA: Request certificate
            CA-->>Proxy: Certificate
            Proxy->>Cache: Store certificate
        else Denied
            Proxy-->>Client: Fail TLS certificate request
        end
    end

Approval endpoints

The endpoint can be a path on the same service or an absolute http or https URL. A local path runs through the service's request chain, so the application can apply its own host rules. The probe sets the request host to the domain being checked and marks the request as internal. This prevents an HTTPS redirect from changing the approval result.

An external endpoint receives a normal HTTP request. The proxy does not follow redirects because a redirect is an endpoint result, not proof that the domain is allowed. Checks have a short time limit, so a slow or unavailable approval service blocks new certificate issuance rather than holding the handshake open without a limit.

References: internal/…/tls_on_demand.go• L22-75 internal/…/tls_on_demand.go• L119-178 internal/…/tls_on_demand_test.go• L22-199

Certificate cache behavior

The approval check applies to domains without a cached certificate. After ACME stores a certificate, later handshakes for that domain skip the approval endpoint and use the cached certificate. This keeps the approval service off the normal handshake path and lets existing domains continue to serve TLS during an approval endpoint outage.

On-demand mode is mutually exclusive with a configured host list, a static certificate, and a canonical host. It must also be used with TLS and on the root path service. These limits keep domain approval, certificate selection, and host redirects from competing with each other.

References: internal/…/tls_on_demand.go• L83-117 internal/…/service.go• L132-160 internal/…/tls_on_demand_test.go• L201-225

HTTP scheme behavior

TLS changes the scheme that the service expects, but it does not by itself remove the HTTP listener. The service can redirect plain HTTP to HTTPS or allow both schemes according to its redirect setting.

The redirect applies to client requests. Internal on-demand probes do not receive this redirect because they must reach the approval handler in the same service. An HTTPS request sent to a service without TLS enabled is rejected instead of being forwarded as secure traffic.

HTTPS redirects

When TLS and TLS redirect are enabled, an HTTP request receives a permanent redirect to the same request URI over HTTPS. When redirect is disabled, both HTTP and HTTPS requests can reach the service.

References: internal/…/service.go• L612-631 internal/…/service.go• L668-708 internal/…/service_test.go• L72-89 internal/…/service_test.go• L137-162

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the router's server-name lookup aligned with service host and root-path rules. A certificate can be valid while request routing still selects no service.
  • Keep static certificate loading and automatic ACME selection mutually clear. A change to certificate options can alter both startup validation and the certificate manager used by the listener.
  • Keep on-demand approval semantics aligned for local paths and external URLs. Both must treat only 200 OK as approval and must pass the requested domain as host.
  • Keep certificate cache scope tied to the ACME directory. A change to ACME authority settings must not cause unrelated cached assets to be reused.
  • Keep internal approval probes outside client redirect rules. Otherwise TLS issuance can fail when --tls-redirect is enabled.

Important failure modes

TriggerConsequenceGuard
TLS is enabled without configured hosts or an on-demand URLService validation fails before it can serve TLS.Set hosts or configure --tls-on-demand-url.
Automatic TLS is configured for a wildcard hostService creation rejects automatic certificate provisioning.Use named hosts or provide a static wildcard certificate.
The certificate file or private key is missing, invalid, or mismatchedStatic certificate setup fails and the service is not created or updated.Use readable PEM files with a matching certificate and key.
An on-demand check returns a non-200 status, times out, or cannot connectACME does not issue a certificate for the requested domain.Make the approval endpoint reachable and return 200 OK only for allowed domains.
A cached domain certificate exists after the approval rule changesThe proxy continues to use the cached certificate without a new approval check.Account for cache state when changing domain access rules.
TLS is set on a non-root path serviceService validation fails because TLS policy must come from the root path service.Put TLS settings on the root path service.
An HTTPS request reaches a service with TLS disabledThe service returns 503 Service Unavailable.Route the host to a TLS-enabled service or use plain HTTP.

Key files

FileWhy to read it
internal/…/service.goDefines TLS options, validates their combinations, creates certificate managers, and applies scheme behavior.
internal/…/tls_on_demand.goDefines the local and external approval flow for new domains.
internal/…/cert.goDefines the static certificate source used by TLS-enabled services.
internal/…/router.goMaps a TLS server name to the service that supplies its certificate.
internal/…/server.goShows how HTTPS and optional HTTP/3 listeners use router certificate lookup.
internal/…/service_map.goShows how root-path services provide shared TLS behavior to path-specific services.
internal/…/deploy.goExposes the TLS, ACME, certificate path, cache, and redirect settings to operators.