我在 docker 中使用 caddy 服务器来反向代理到我的 Nodejs 服务器。我正在传递
UPSTREAMS
env var,如Here所述。但 UPSTREAMS 不被球童认可。如果我在 Caddyfile 中对 UPSTREAMS 进行硬编码,reverse_proxy 就可以正常工作。
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
还有我的 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."
}
}
提示位于您链接的 Caddy 文档中...
您需要将它们设置为 Dockerfile 或构建参数中的这种形式的环境变量在 Caddyfile 解析开始之前被替换
ENV
条目。但是...
如果您想将环境变量的替换推迟到运行时,您可以使用 标准
尝试用这个代替{env.*}
占位符
to {env.UPSTREAMS}