worker_processes auto; events { worker_connections 1024; } http { upstream api_backend { # least_conn лучше распределяет запросы, чем round-robin, # особенно когда одна реплика чуть медленнее. least_conn; server api1:8080 max_fails=1 fail_timeout=2s; server api2:8080 max_fails=1 fail_timeout=2s; } server { listen 8080; # Проверка, что LB жив (без проксирования в API) location = /lb-health { access_log off; return 200 'ok'; add_header Content-Type text/plain; } location / { proxy_pass http://api_backend; proxy_http_version 1.1; 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; # Важно для быстрого отключения упавшей реплики proxy_next_upstream error timeout http_502 http_503 http_504; proxy_connect_timeout 2s; proxy_read_timeout 10s; } } }