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; # Redirect on 502/503 to the next available replica once, then give up. proxy_next_upstream error timeout http_502 http_503; proxy_next_upstream_tries 1; 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; } } }