events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; # Upstream group: both API replicas. Health-checked by Nginx so a stopped # replica is taken out of rotation automatically (max_fails + fail_timeout). upstream api { server api-1:8080 max_fails=3 fail_timeout=10s; server api-2:8080 max_fails=3 fail_timeout=10s; } server { listen 8080; server_name _; # Keep the instance header so clients can see which replica answered. 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; # Never wait forever on a dead replica. proxy_connect_timeout 2s; proxy_read_timeout 10s; proxy_send_timeout 10s; # A refused connection/502 means the selected replica is unavailable and # can be retried on its peer. A structured 503 is an application # dependency failure (for example PostgreSQL down), not an unhealthy # replica: propagating it preserves the API error envelope instead of # marking both live peers down and manufacturing an Nginx 502. proxy_next_upstream error timeout http_502; # Two attempts cover both configured replicas: a request that lands on # the stopped instance is retried once on its healthy peer. proxy_next_upstream_tries 2; location / { proxy_pass http://api; } location /health/ready { # Readiness is checked by Compose on the LB itself; forward to api. proxy_pass http://api; } location /version { proxy_pass http://api; } location /info { proxy_pass http://api; } } }