# nginx load balancer for the scaled "api" service in docker-compose.scale.yml.
# Uses Docker's embedded DNS resolver so newly scaled replicas are discovered
# without editing a static upstream list.

server {
    listen 80;

    # Docker embedded DNS. valid=10s re-resolves so scaling up/down is picked up.
    resolver 127.0.0.11 valid=10s;

    location / {
        # Use a variable so nginx defers resolution to request time (round-robin
        # across all replicas of the "api" service).
        set $api_upstream http://api:3001;
        proxy_pass $api_upstream;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Server-Sent Events: do not buffer the payment status stream.
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 1h;
        proxy_set_header Connection "";
        proxy_http_version 1.1;
    }
}
