Caddy’s headline use case is HTTP, but the caddy-l4 plugin turns the same binary into a raw TCP and UDP proxy. The use cases: SSH bastions, SMTP MX relays, PostgreSQL pgbouncer fronts, gRPC traffic that needs port-level routing before HTTP/2 framing, MQTT brokers, anything that speaks TCP and is not HTTP. This article is the production configuration we deploy for clients running multi-protocol edges on a single Caddy host.
How to verify
A working L4 proxy responds at the configured port and forwards bytes:
nc -zv ssh-bastion.example.com 22
ssh -p 22 user@ssh-bastion.example.com
nc -u dns.example.com 53 < query.bin | xxd | head
sudo journalctl -u caddy --since '5m ago' | grep -i 'layer4\|connection'
caddy list-modules | grep -i layer4
If caddy-l4 is compiled in, list-modules shows entries under layer4.*. If not, the JSON config will fail to load with “module not found.”
What’s happening
caddy-l4 is a plugin that adds Layer 4 proxying to Caddy. It registers a top-level app (named layer4) that listens on configured TCP/UDP ports, accepts connections, optionally matches against the early bytes of the stream, and forwards to an upstream. Unlike HTTP routing (which terminates and re-emits the request), L4 proxying is byte-for-byte transparent.
The matcher system lets one Caddy instance serve multiple protocols on a single port. For example, on port 443 Caddy can detect TLS (a TLS handshake’s first bytes are structured), inspect the SNI, and route SSH traffic in a TLS-wrapped tunnel to one backend while sending plain HTTPS to the standard HTTP app. The tls matcher inspects the ClientHello without terminating TLS — the TLS handshake completes against the upstream.
The plugin’s matchers cover:
tls— match by SNI or by ALPNhttp— match the early HTTP request line (works on plaintext)ssh— match the SSH protocol bannerxmpp,socks4,socks5,proxy_protocol— protocol-specific matchersip— match by source IP CIDRnot,or,and— logical combinators
The L4 app is configured via JSON only — there is no Caddyfile DSL for L4. See Caddyfile vs JSON for the format trade-offs.
The procedure
-
Build Caddy with the L4 plugin. Stock Caddy does not include it:
xcaddy build \ --with github.com/mholt/caddy-l4 \ --output /usr/local/bin/caddySee xcaddy custom builds for the full pattern.
-
Configure a simple TCP forward. JSON for a Caddy that proxies port 5432 (PostgreSQL) to a private upstream:
{ "apps": { "layer4": { "servers": { "postgres": { "listen": [":5432"], "routes": [{ "handle": [{ "handler": "proxy", "upstreams": [{ "dial": ["pg-primary.internal:5432"] }] }] }] } } } } }POST to
http://localhost:2019/loador save as/etc/caddy/caddy.jsonif running thecaddy-apisystemd unit. -
SSH on 22 with active health-checked upstreams. Three SSH bastions behind one VIP:
{ "apps": { "layer4": { "servers": { "ssh": { "listen": [":22"], "routes": [{ "match": [{ "ssh": {} }], "handle": [{ "handler": "proxy", "upstreams": [ { "dial": ["bastion-1.internal:22"] }, { "dial": ["bastion-2.internal:22"] }, { "dial": ["bastion-3.internal:22"] } ], "load_balancing": { "selection_policy": { "policy": "round_robin" } } }] }] } } } } }The
sshmatcher accepts the connection only if the early bytes match the SSH banner — protects the port from non-SSH probes. -
Multi-protocol on port 443. TLS-encapsulated HTTP plus SSH-over-TLS using SNI-based routing:
{ "apps": { "layer4": { "servers": { "edge": { "listen": [":443"], "routes": [ { "match": [{ "tls": { "sni": ["ssh.example.com"] } }], "handle": [{ "handler": "proxy", "upstreams": [{ "dial": ["bastion-1.internal:22"] }] }] }, { "match": [{ "tls": {} }], "handle": [{ "handler": "tls" }, { "handler": "proxy", "upstreams": [{ "dial": ["http-backend.internal:80"] }] }] } ] } } } } }First route catches SNI
ssh.example.comand forwards encrypted bytes to the SSH bastion. Second route catches all other TLS, terminates it (using Caddy’s automatic certs), and forwards plain HTTP. -
UDP proxy. DNS or QUIC traffic:
{ "apps": { "layer4": { "servers": { "dns": { "listen": ["udp/:53"], "routes": [{ "handle": [{ "handler": "proxy", "upstreams": [ { "dial": ["dns-1.internal:53"] }, { "dial": ["dns-2.internal:53"] } ] }] }] } } } } }The
udp/prefix on the listener address makes it UDP. The proxy handler supports both protocols. -
PROXY protocol upstream. When the backend wants the real client IP and supports the PROXY protocol header:
{ "handle": [{ "handler": "proxy", "proxy_protocol": "v2", "upstreams": [{ "dial": ["nginx-backend.internal:443"] }] }] }The backend (HAProxy, Nginx with
proxy_protocol on, another Caddy with the matcher) reads the PROXY header and logs the real source IP instead of Caddy’s egress IP.
Operational notes
- L4 proxying is byte-for-byte transparent. There is no header rewriting, no HTTP keep-alive coalescing, no per-request load balancing — the entire connection goes to one upstream, and stays there until the client disconnects.
- The TLS matcher inspects the ClientHello and routes based on SNI without terminating. The upstream still does the TLS handshake. For terminating TLS at the L4 layer, use the
tlshandler — but then you need cert state for the SNI names, the same as for HTTP sites. - Health checks in L4 are passive only — there is no active health check loop like the HTTP
health_uri. A bad upstream is removed from rotation after a connection failure and re-added afterfail_duration. - The L4 app and the HTTP app can listen on different ports in the same Caddy process. Run SSH proxy on 22, MQTT on 1883, HTTP on 80/443 — one binary, one config, one set of metrics.
- The plugin’s metrics integrate with Caddy’s Prometheus exporter —
caddy_layer4_connections_totaland friends are available alongside the HTTP metrics. See Caddy observability metrics.
Stack Harbor runs multi-protocol edges on Caddy with the L4 plugin for clients consolidating SSH bastions, SMTP relays, and HTTPS on a single host — the matcher rules under git, the upstream health auditable, and the cert state shared across both L4 and HTTP apps. If you have an edge that has accumulated separate HAProxy and Nginx layers because the protocols are mixed, this is the kind of work covered by our Managed Operations practice.