caddyserver/caddy · error

weight of an upstream cannot be negative

Error message

weight of an upstream cannot be negative

What it means

Validation guard in WeightedRoundRobinSelection.Validate: an individual upstream weight in the weighted_round_robin policy is negative. The faulty input is the offending weight value in the policy configuration; weights must be non-negative.

Solutions

  1. Set all upstream weights to zero or a positive value.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at modules/caddyhttp/reverseproxy/selectionpolicies.go:137 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/65b2b6044af37a84. Report an issue: GitHub.

Appendix: source

Thrown at modules/caddyhttp/reverseproxy/selectionpolicies.go:137

	return nil
}

// Provision sets up r.
func (r *WeightedRoundRobinSelection) Provision(ctx caddy.Context) error {
	for _, weight := range r.Weights {
		r.totalWeight += weight
	}
	return nil
}

// Validate ensures that r's configuration is valid
func (r *WeightedRoundRobinSelection) Validate() error {
	if r.totalWeight <= 0 {
		return fmt.Errorf("weighted_round_robin requires at least one upstream with a positive weight")
	}
	for _, weight := range r.Weights {
		if weight < 0 {
			return fmt.Errorf("weight of an upstream cannot be negative")
		}
	}
	return nil
}

// Select returns an available host, if any.
func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request, _ http.ResponseWriter) *Upstream {
	if len(pool) == 0 {
		return nil
	}
	if len(r.Weights) < 2 {
		return pool[0]
	}
	var index, totalWeight int
	var weights []int

	for _, w := range r.Weights {
		if w > 0 {

View on GitHub (pinned to 50e54ee279)