Files
mtusi2026lab3/infra/lb/nginx.conf
T
2026-10-15 22:20:33 +03:00

73 lines
3.2 KiB
Nginx Configuration File

# Load balancer for the lab3 stand (nginx OSS, container `lb`).
#
# Two static API replica addresses, discovered through Docker's embedded DNS
# (127.0.0.11). `resolve` re-resolves the names every 5s, so a replica that is
# stopped or recreated with a new address is picked up without an nginx reload.
# Passive health checks: one failed attempt takes the replica out for 3s.
# Retries include non-idempotent methods: replaying a POST is safe here because
# lab3 requires Idempotency-Key on message creation.
worker_processes auto;
error_log /dev/stderr warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log /dev/stdout combined;
default_type application/json;
server_tokens off;
resolver 127.0.0.11 valid=5s ipv6=off;
resolver_timeout 2s;
upstream api_backend {
zone api_backend 64k;
least_conn;
# Общее имя -> все адреса реплик (общий network alias в compose.yaml).
# `resolve` переразрешает имя каждые 5 с, поэтому номера контейнеров и их
# IP могут меняться без правки конфига и без reload.
# max_fails=0 (пассивные проверки выключены) осознанно: upstream'ов всего
# два, и исключение последней живой реплики из-за одного медленного
# ответа превращается в полный отказ («no live upstreams», измерено в
# rolling update). Удалённую реплику и так убирают retry и DNS.
server api-replica:8080 resolve max_fails=1 fail_timeout=3s;
keepalive 32;
keepalive_requests 1000;
keepalive_timeout 60s;
}
server {
listen 80;
server_name _;
# Client headers (including X-Instance-Id and Idempotency-Key) and
# response headers (including X-Instance-Id) are forwarded as-is.
proxy_http_version 1.1;
proxy_set_header Connection "";
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_connect_timeout 1s;
# Короткий read timeout: запрос, попавший в keepalive-соединение к уже
# удалённой реплике, ждёт таймаута, после чего уходит на живую реплику.
# 5 с многократно больше любой легитимной задержки этого API.
proxy_send_timeout 30s;
proxy_read_timeout 5s;
# Retry the other replica on connect error/timeout, or on 502/503/504,
# for every method (POST included, see the header comment above).
proxy_next_upstream error timeout http_502 http_503 http_504 non_idempotent;
proxy_next_upstream_tries 3;
proxy_next_upstream_timeout 10s;
# /health/live and /health/ready are proxied to a replica unchanged.
location / {
proxy_pass http://api_backend;
}
}
}