caddyserver/caddy · error
server block %v: %v
Error message
server block %v: %v
What it means
A wrapper error from compileEncodedMatcherSets: encoding one of the server block's compiled matcher sets into a caddy.ModuleMap failed. The inner error typically comes from encodeMatcherSet rejecting a matcher module whose JSON cannot be produced, and the message prefixes the block's keys so you know which site block is at fault.
Source
Thrown at caddyconfig/httpcaddyfile/httptype.go:1663
for _, mp := range matcherPairs {
matcherSet := make(map[string]caddyhttp.RequestMatcherWithError)
if len(mp.hostm) > 0 {
matcherSet["host"] = mp.hostm
}
if len(mp.pathm) > 0 {
matcherSet["path"] = mp.pathm
}
if len(matcherSet) > 0 {
matcherSets = append(matcherSets, matcherSet)
}
}
// finally, encode each of the matcher sets
matcherSetsEnc := make([]caddy.ModuleMap, 0, len(matcherSets))
for _, ms := range matcherSets {
msEncoded, err := encodeMatcherSet(ms)
if err != nil {
return nil, fmt.Errorf("server block %v: %v", sblock.block.Keys, err)
}
matcherSetsEnc = append(matcherSetsEnc, msEncoded)
}
return matcherSetsEnc, nil
}
func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.ModuleMap) error {
d.Next() // advance to the first token
// this is the "name" for "named matchers"
definitionName := d.Val()
if _, ok := matchers[definitionName]; ok {
return fmt.Errorf("matcher is defined more than once: %s", definitionName)
}
matchers[definitionName] = make(caddy.ModuleMap)
View on GitHub (pinned to 50e54ee279)
Solutions
- Read the inner error — it identifies the failing matcher value; correct it in the named site block
- If a custom matcher plugin is involved, fix its JSON marshaling or update the plugin
- Reproduce with `caddy adapt` on the single block to minimize input
- Simplify the block's matchers and re-add them one by one to isolate the bad one
Defensive patterns
Strategy: try-catch
Validate before calling
// Programmatically: round-trip each matcher set before use
if _, err := json.Marshal(matcherSet); err != nil {
return fmt.Errorf("block %v: unencodable matcher: %w", keys, err)
} Try / catch
msEncoded, err := encodeMatcherSet(ms)
if err != nil {
return nil, fmt.Errorf("server block %v: %w", sblock.block.Keys, err)
} Prevention
- For custom matcher modules, implement/verify MarshalJSON and test round-trip
- Validate generated configs with caddy adapt before loading
- Keep matcher values to plain strings/placeholders the stock encoder handles
When it happens
Trigger: A site block whose host/path matcher combination includes values that fail encoding — e.g. a placeholder or regexp that cannot marshal into the module map; practically rare with stock matchers, more likely with custom matcher modules returning non-serializable configs from their UnmarshalCaddyfile.
Common situations: Custom matcher plugins with broken MarshalJSON, or malformed hostnames/placeholders in site keys; also seen when a config generated by tooling feeds unexpected values into matcher fields.
Related errors
- unrecognized matcher name: %+v
- matcher is defined more than once: %s
- missing 'req' argument
- unsupported type
- private key does not match issuer public key
AI-assisted analysis of caddyserver/caddy@50e54ee279 (2026-08-15).
Data as JSON: /api/errors/4b1f58e0a1739e00.
Report an issue: GitHub.