user nginx; worker_processes auto; pid /var/run/nginx.pid; error_log /dev/stderr warn; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /dev/stdout; server_tokens off; client_max_body_size 1m; sendfile on; keepalive_timeout 65; # Two interchangeable API replicas. Passive health checks: one failed # connection marks a backend unavailable for fail_timeout (5 s). upstream api_backend { server api1:8080 max_fails=1 fail_timeout=5s; server api2:8080 max_fails=1 fail_timeout=5s; keepalive 16; } server { listen 8080; server_name _; # Message creation is the only POST that is safe to retry at the LB: # the same Idempotency-Key makes the second attempt return the same # message. This regex location also covers GET history (idempotent). location ~ ^/api/v1/chats/[^/]+/messages$ { proxy_pass http://api_backend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 1s; proxy_send_timeout 5s; proxy_read_timeout 10s; proxy_next_upstream error timeout http_502 http_503 http_504 non_idempotent; proxy_next_upstream_tries 2; } # Registration, login and chat creation are not retried transparently: # without an idempotency contract a retry could create a second object. location / { proxy_pass http://api_backend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 1s; proxy_send_timeout 5s; proxy_read_timeout 10s; proxy_next_upstream error timeout http_502 http_503 http_504; proxy_next_upstream_tries 2; } } }