I'm using caddy server in docker to reverse proxy to my nodejs servers. I'm passing UPSTREAMS
env var as documented Here. But the UPSTREAMS are not recognized by caddy. reverse_proxy works fine if I hardcode the UPSTREAMS in Caddyfile.
Dockerfile
# https://caddyserver.com/docs/build#docker
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
docker-compose.yaml
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
And my Caddyfile
{
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."
}
}
Removing the quotes from UPSTREAMS env var solved the problem
environment:
- UPSTREAMS=node-server-1:5000 node-server-2:5000