UPSTREAMS env var not being processed

1. The problem I’m having:

Hi, I’m trying to pass UPSTREAMS env var to Caddyfile as documented here. But it doesn’t seem to work. It works fine when I hard code upstreams in reverse_proxy.

2. Error messages and/or full log output:

3. Caddy version:

v2.8.4

4. How I installed and ran Caddy:

I’m using caddy inside docker container.

c. Service/unit/compose file:

Here is my dockerfile

FROM caddy:2.8.4-builder AS builder

RUN xcaddy build \
    --with github.com/mholt/caddy-ratelimit

FROM caddy:2.8.4

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

and my docker-compose

services:
  # https://caddyserver.com/docs/running#setup
  caddy:
    build:
      context: .
      dockerfile: Dockerfile.caddy
    container_name: caddy
    restart: unless-stopped
    environment:
      - DOMAIN=${DOMAIN:-http://localhost:80}
      - CERT_EMAIL=$CERT_EMAIL
      - UPSTREAMS="node-server-1:5000 node-server-2:5000"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./caddy_data:/data
      - ./caddy_config:/config
    ports:
      - 80:80
      - 443:443

d. My complete Caddy config:

{
	email {$CERT_EMAIL}
	# https://github.com/mholt/caddy-ratelimit?tab=readme-ov-file#caddyfile-config
	order rate_limit before basicauth

	log {
		level ERROR
	}
}

{$DOMAIN} {
	rate_limit {
		zone myzone {
			key {remote_ip}
			events 20
			window 1m
		}
	}

	handle {
		reverse_proxy {
			# THIS WORKS
			# to node-server-1:5000 node-server-2:5000


			# THIS DOESN'T WORK
         		to {$UPSTREAMS}


			# https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#load-balancing
			lb_policy round_robin

			lb_try_duration 1s
		}
	}

	# https://caddyserver.com/docs/caddyfile/directives/handle_errors#examples
	handle_errors 429 {
		respond "You're being rate limited. Please try again in 1 minute."
	}
}

5. Links to relevant resources:

What do you mean by this? Show evidence. Show the symptoms. Show your logs. Don’t skip parts of the help topic template.

Please never say “doesn’t work” when you’re asking for help, it’s meaningless. Be specific.

I think the problem is the quotes. I think this causes the quotes to be kept in the env var, so you end up with to "node-server-1:5000 node-server-2:5000" which is one token, not two.

1 Like

You’re right. Removing the quotes solved the problem

    environment:
      - UPSTREAMS=node-server-1:5000 node-server-2:5000

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.