worker_processes 1; pid /tmp/nginx.pid; error_log /dev/stderr warn; events { worker_connections 1024; } http { resolver 127.0.0.11 valid=2s ipv6=off; resolver_timeout 1s; log_format upstream_json escape=json '{"time":"$time_iso8601","request_id":"$request_id",' '"method":"$request_method","uri":"$uri","status":$status,' '"request_time":$request_time,"upstream_addr":"$upstream_addr",' '"upstream_status":"$upstream_status",' '"upstream_response_time":"$upstream_response_time",' '"instance":"$upstream_http_x_instance_id"}'; access_log /dev/stdout upstream_json; server_tokens off; client_max_body_size 1m; client_body_buffer_size 64k; client_header_timeout 10s; client_body_timeout 10s; send_timeout 10s; keepalive_timeout 65s; upstream api { zone api 64k; server api:8080 resolve max_fails=1 fail_timeout=5s; keepalive 32; # Меньше APP_KEEPALIVE_TIMEOUT_SEC (75 с): idle-соединение первым # закрывает nginx, а не реплика, — нет гонки «запрос в закрытое соединение» keepalive_timeout 60s; } proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; # Один ID на LB и реплику: API принимает $request_id и возвращает его proxy_set_header X-Request-Id $request_id; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # connect внутри bridge-сети завершает ядро за доли мс даже у занятой реплики. # Самый долгий запрос API: ожидание пула 3 с + statement_timeout 5 с proxy_connect_timeout 500ms; proxy_send_timeout 10s; proxy_read_timeout 10s; # Повтор на другой реплике только при ошибке соединения/таймауте. # POST, уже отправленный реплике, nginx не повторяет (non_idempotent выключен): # повтор сообщения делает клиент с тем же Idempotency-Key. # 503 приложения (общая зависимость недоступна) не повторяется и не исключает # реплику: другая реплика зависит от той же БД. proxy_next_upstream error timeout; proxy_next_upstream_tries 3; proxy_next_upstream_timeout 5s; server { listen 8080; location / { proxy_pass http://api; } error_page 502 503 504 = @unavailable; error_page 413 = @too_large; include /etc/nginx/errors.conf; } server { listen 8081; # Реплика api-short-ttl только для make test-expiry (профиль expiry): # адрес резолвится на каждый запрос, без профиля — 503 от LB location / { set $api_short_ttl http://api-short-ttl:8080; proxy_pass $api_short_ttl; } error_page 502 503 504 = @unavailable; error_page 413 = @too_large; include /etc/nginx/errors.conf; } # Liveness самого балансировщика для healthcheck контейнера; наружу не публикуется server { listen 127.0.0.1:8090; access_log off; location = /healthz { default_type text/plain; return 200 "ok\n"; } } }