caddyserver/caddy · error

cannot have duplicate named_routes

Error message

cannot have duplicate named_routes: %s

What it means

While extracting named routes in extractNamedRoutes, two server blocks defined a named route with the same name; since names must be unique across the config, Setup rejects it listing the duplicate name.

Solutions

  1. Rename one of the duplicate named routes so each name is unique across the Caddyfile.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at caddyconfig/httpcaddyfile/httptype.go:531 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at caddyconfig/httpcaddyfile/httptype.go:531

		handler, err := ParseSegmentAsSubroute(h)
		if err != nil {
			return nil, err
		}
		subroute := handler.(*caddyhttp.Subroute)
		route := caddyhttp.Route{}

		if len(subroute.Routes) == 1 && len(subroute.Routes[0].MatcherSetsRaw) == 0 {
			// if there's only one route with no matcher, then we can simplify
			route.HandlersRaw = append(route.HandlersRaw, subroute.Routes[0].HandlersRaw[0])
		} else {
			// otherwise we need the whole subroute
			route.HandlersRaw = []json.RawMessage{caddyconfig.JSONModuleObject(handler, "handler", subroute.CaddyModule().ID.Name(), h.warnings)}
		}

		key := sb.block.GetKeysText()[0]
		if _, exists := namedRoutes[key]; exists {
			return nil, fmt.Errorf("cannot have duplicate named_routes: %s", key)
		}
		namedRoutes[key] = &route

		// remember which named routes this named route invokes,
		// so they can also be attached to any server that invokes
		// this one
		if state[namedRouteKey] != nil {
			for name := range state[namedRouteKey].(map[string]struct{}) {
				namedRouteInvokes[key] = append(namedRouteInvokes[key], name)
			}
			state[namedRouteKey] = nil
		}
	}
	options["named_routes"] = namedRoutes
	options["named_route_invokes"] = namedRouteInvokes

	return filtered, nil
}

View on GitHub (pinned to 50e54ee279)