caddyserver/caddy · error

expect_body: compiling regular expression: %v

Error message

expect_body: compiling regular expression: %v

What it means

During provisioning of active health checks, the `expect_body` value is compiled as a Go regexp (`regexp.Compile`). If the pattern is invalid Go regexp syntax, provisioning fails with the underlying compile error, and the reverse_proxy module (and the whole config load) errors out.

Source

Thrown at modules/caddyhttp/reverseproxy/healthchecks.go:209

		// then the port is ignored.
		if a.Upstream != "" {
			upstream.activeHealthCheckUpstream = a.Upstream
		} else if a.Port != 0 {
			// if there's an alternative port for health-check provided in the config,
			// then use it, otherwise use the port of upstream.
			upstream.activeHealthCheckPort = a.Port
		}
	}

	if a.Interval == 0 {
		a.Interval = caddy.Duration(30 * time.Second)
	}

	if a.ExpectBody != "" {
		var err error
		a.bodyRegexp, err = regexp.Compile(a.ExpectBody)
		if err != nil {
			return fmt.Errorf("expect_body: compiling regular expression: %v", err)
		}
	}

	if a.Passes < 1 {
		a.Passes = 1
	}

	if a.Fails < 1 {
		a.Fails = 1
	}

	return nil
}

// IsEnabled checks if the active health checks have
// the minimum config necessary to be enabled.
func (a *ActiveHealthChecks) IsEnabled() bool {
	return a.Path != "" || a.URI != "" || a.Port != 0

View on GitHub (pinned to 50e54ee279)

Solutions

  1. Test the pattern with a Go regexp tool or `go run` snippet before putting it in config
  2. Remove PCRE-only features (lookahead/lookbehind/backreferences) — Go RE2 does not support them
  3. Escape literal braces and dots: \{ \. e.g. expect_body "status.*ok"
  4. Simplify to a substring that is plain text, since expect_body only needs to match part of the body

Example fix

# before
reverse_proxy localhost:9000 {
	health_uri /health
	expect_body "{\"status\":\"ok\"}"
}
# after
reverse_proxy localhost:9000 {
	health_uri /health
	expect_body "status":"ok"
}
Defensive patterns

Strategy: validation

Validate before calling

if health.ExpectBody != "" {
	if _, err := regexp.Compile(health.ExpectBody); err != nil {
		return fmt.Errorf("expect_body invalid: %w", err)
	}
}

Prevention

When it happens

Trigger: Setting `health_uri`-style active checks with `expect_body` to a string with invalid Go regex syntax: unbalanced parens, trailing backslash, invalid character class like `[z-a]`, or PCRE-only constructs like lookaheads `(?=...)`.

Common situations: Porting regexes from nginx/PCRE or JavaScript that use lookaheads/lookbehinds (unsupported in Go's RE2); unescaped braces intended literally, e.g. matching JSON bodies like `{"status":"ok"}`.

Related errors


AI-assisted analysis of caddyserver/caddy@50e54ee279 (2026-08-15). Data as JSON: /api/errors/cd196f5ce9331d88. Report an issue: GitHub.