Proxy (embedded Caddy)
Caddy runs in-process as a library. There is no Caddyfile. All config is JSON, built programmatically and pushed via caddy.Load(). Source: /internal/proxy/.
Lifecycle
Section titled “Lifecycle”NewCaddyProxy(cfg)allocates the proxy struct (no Caddy started yet).- The reconciler calls
ResolveRoutes(app, resolver)for each app and collects the routes. The resolver (DockerResolverin /internal/proxy/resolver.go) answers<service>:<port>upstreams by looking up the running container’s IP on thesimpledeploy-publicnetwork via Docker API; host-port-backed endpoints skip the resolver entirely and resolve tolocalhost:<host_port>. - The reconciler calls
SetRoutes(routes)with the assembled table. SetRoutesskips (with a warning) any route whose domain fails^[a-zA-Z0-9][a-zA-Z0-9.*-]*$, so one bad label cannot block every app’s routes. Each domain is served by one app (claimDomains). The app that already serves it keeps it while it still claims it. Otherwise, including after a restart (the owner map starts empty), the app created first wins: the lowestRoute.AppID(the store ID, set by the reconciler; apps not in the store yet sort last), with the slug as tie-break. Route order plays no part. Other apps’ routes on that domain are dropped with a warning. The per-domain rate-limit and IP-allowlist registries are then replaced as a whole (domains lowercased, port and trailing dot stripped), so one app can never clear or override another app’s rules, and the config is reloaded. If the load fails, the routes, domain owners and both registries revert to the set Caddy is still running, so rules keep matching the live config. Handlers carry their route’s domain in the Caddy JSON and look up that exact key; Host-based lookup (exact first, then wildcards) is only a fallback. The dashboard’s configdomainis not filtered here: the API refuses it as an endpoint domain for users who are notsuper_admin(checkEndpointDomainsin /internal/api/domain.go).- The reload runs
buildConfig()to produce the full JSON, marshals it, and callscaddy.Load(data, true)unless the JSON and the size/mtime of every referenced custom cert file match the last successful load (route order is deterministic, so no-op redeploys skip the reload). Cert upload/delete callForceReload()to reload unconditionally. Caddy hot-reloads in-place; in-flight requests complete on the old config, new requests use the new one. On Linux each reload binds a newSO_REUSEPORTsocket and closes the old one, so connections queued on the old socket but not yet accepted are reset (curl: (35) ... Connection reset by peer). Setnet.ipv4.tcp_migrate_req=1(kernel 5.14+) to have the kernel migrate them instead.
Stop() calls caddy.Stop(). The admin endpoint is always disabled (admin.disabled: true); the only way to change config is via this code path.
buildConfig
Section titled “buildConfig”Located in /internal/proxy/proxy.go, it returns a map[string]interface{} shaped like Caddy’s JSON schema.
For each Route:
- Routes are first passed through
orderRoutes(/internal/proxy/match.go): grouped by domain in first-seen order; inside a domaingrpcroutes, then path routes (longest first), then the catch-all. - One Caddy route entry per
Route,terminal: true, with matcher fromrouteMatcher:host, pluspathwhen set, plus forgrpcaheadermatcherContent-Type: application/grpc*and anotmatcher excludingapplication/grpc-web*. - A handler chain in this exact order:
simpledeploy_ipaccess,simpledeploy_ratelimit,simpledeploy_metrics, oneheadershandler per default security header,timeouts(HTTP routes only), thenreverse_proxytor.Upstream. Forh2c/grpcroutesreverse_proxygetstransport: {protocol: http, versions: [h2c]}andflush_interval: -1. - Default security headers (
securityHeaderHandlers):X-Content-Type-Options: nosniff,X-Frame-Options: SAMEORIGIN,Referrer-Policy: strict-origin-when-cross-origin, plusStrict-Transport-Security: max-age=31536000; includeSubDomainswhen the route’stlsis notoff. Each is added only when the app’s response does not already set that header, so an app’s own value always wins. Each header gets its ownheadershandler withresponse.setandresponse.require: {headers: {<name>: null}}(a null value matches only a missing field;requirealso defers the check until the upstream’s headers are known). One handler per header because a handler’srequiregates all its operations at once. Responses Caddy generates itself on errors (e.g. a 502 when the upstream is down) get none of these. - Request limits on every server (
proxy,proxy_extra,proxy_http):max_header_bytesis 1 MiB (Caddy’s own default is 16 KiB, which answers 431 before any handler runs). Caddy’s server-wide request body idle timeout is off (read_idle_timeout: -1) because it would reset gRPC client-streaming and bidi RPCs whose client stays quiet. HTTP routes get the same protection back from a per-routetimeoutshandler (read_timeout1m): an upload that sends nothing for a minute is cut.h2c/grpcroutes have no body idle limit. Caddy’s default 1m write idle timeout stays on; it only applies while a write is in progress, so SSE and long polls are unaffected. - Caddy drops request header names that contain
_or.before routing, so apps never see them andsimpledeploy.ratelimit.by: header:NAMEonly works for names made of letters, digits and-. - If TLS mode is
custom, append a load-files entry (once per domain) pointing at<app_dir>/certs/<domain>.crtand.keywithtags: [domain]. - When the listener is plain HTTP (
tls.mode: offand no per-routelocal), the server setsprotocols: [h1, h2, h2c]so gRPC clients can use prior-knowledge h2c. - The
proxyserver listens onlisten_addrwith Caddy’s default protocols (h1, h2, h3).extra_listen_addrsrun as a separateproxy_extraserver with the same routes, TLS connection policies and automatic HTTPS settings, but protocolsh1/h2only (h1/h2/h2cwhen TLS is off). Certificates are shared through Caddy’s cert cache. Keeping HTTP/3 off the extra server stops long-lived h2 streams on that port from holding an old server whose QUIC listeners are already closed, which loggedsetting HTTP/3 Alt-Svc header ... no port can be announcedon every reload.
The whole thing is then assembled into a single HTTP server listening on the configured listenAddr (typically :443).
TLS automation policies
Section titled “TLS automation policies”The tlsCfg block changes shape based on tlsMode:
| Mode | What buildConfig emits |
|---|---|
auto (with email) |
tls.automation.policies[0].issuers[0] = {module: acme, email: <tlsEmail>}. ACME flow uses HTTP-01 challenge on :80. |
local |
Same shape, issuer module is internal (Caddy’s local CA). Storage root is set to <dataDir>/caddy. |
custom |
No automation policy. Caddy serves whatever was loaded via load_files. |
off |
server.automatic_https.disable: true. HTTP only. |
Modes can mix per route: a single proxy can serve some routes with ACME, others with custom certs, others HTTP-only by setting simpledeploy.endpoints.N.tls.
Custom Caddy modules
Section titled “Custom Caddy modules”All three are registered in init() of their respective files in /internal/proxy/. They are real Caddy modules (http.handlers.simpledeploy_*), not custom HTTP middleware bolted on the side. This means Caddy’s request lifecycle (logging, error handling, response recording) wraps them correctly.
simpledeploy_metrics
Section titled “simpledeploy_metrics”/internal/proxy/reqmetrics.go. Wraps the response writer to capture the final status code (1xx informational responses are skipped, 101 counts), measures latency from before to after next.ServeHTTP, then non-blocking send into RequestStatsCh (a package-level chan<- set during startup). Dropped if full. When the chain returns an error before anything was written, Caddy’s error handling writes the response outside this wrapper, so the status comes from the error instead: the caddyhttp.HandlerError status (502 for an unreachable upstream, 504 for a timeout), 499 when the client went away, else 500. Once a status or body was written, that status is kept even if an error follows. A client disconnect that reverse_proxy turns into WriteHeader(499) is recorded as 499. Path is normalized via NormalizePath (numeric IDs and UUIDs become {id}) so the metrics table does not explode in cardinality.
simpledeploy_ratelimit
Section titled “simpledeploy_ratelimit”/internal/proxy/ratelimit.go. Per-domain configs registered via RateLimiters.Set(domain, cfg) from SetRoutes. Each domain has its own domainLimiter keyed by client IP (default), header value, or path depending on simpledeploy.ratelimit.by. Each bucket allows requests per window; a window starts at the bucket’s first request after the previous one ended. With burst > 0, up to burst more requests pass once the window’s requests are used up. The burst refills only after a whole window within requests (or with no requests), so a client that keeps going over is held to requests per window. burst 0 (label unset) is a plain fixed window. Each domain keeps at most 10,000 buckets: when full, buckets idle for a whole window are evicted first, then the least recently used. Keys longer than 128 bytes are stored as their SHA-256.
simpledeploy_ipaccess
Section titled “simpledeploy_ipaccess”/internal/proxy/ipaccess.go. Allowlist of IPs and CIDRs from simpledeploy.access.allow. Validated as net.ParseIP or net.ParseCIDR before being added; invalid entries are logged and skipped.
ACME flow
Section titled “ACME flow”When tlsMode = auto, Caddy obtains certs on first request to a new domain. The HTTP-01 challenge requires port 80 to be reachable from the public Internet for the apex of each registered domain. SimpleDeploy does not bind 80 directly; Caddy’s default HTTP server picks it up because Caddy’s automatic_https is enabled. Issued certs are stored under <dataDir>/caddy/ and reused across restarts.
Why no Caddyfile
Section titled “Why no Caddyfile”Caddyfile is fine for static config but a poor fit when routes change at runtime in response to file events. JSON is Caddy’s native format, fully round-trippable, and caddy.Load() is faster than re-parsing a Caddyfile. It also means SimpleDeploy never has to escape user input into a DSL.
Testing
Section titled “Testing”MockProxy in /internal/proxy/mock.go implements the same interface and just records the routes it was given, so tests can assert on the route table without starting Caddy.