worker_processes auto; worker_shutdown_timeout 10s; events { worker_connections 1024; } http { # Let the application enforce the exact 10 MiB contract and return JSON. client_max_body_size 11m; client_body_timeout 10s; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log warn; # Docker embedded DNS returns current Compose replicas. Nginx OSS 1.27.3+ # refreshes these addresses without a configuration reload. resolver 127.0.0.11 valid=2s ipv6=off; resolver_timeout 1s; upstream api_backend { zone api_backend 64k; least_conn; server api:8080 resolve max_fails=1 fail_timeout=2s; keepalive 32; } server { listen 80; server_name _; proxy_intercept_errors on; error_page 502 503 504 = @no_ready_api; location = /nginx-health { access_log off; default_type application/json; return 200 '{"status":"ok"}'; } # After both peers have refused traffic or become unreachable, Nginx # would normally emit its own HTML 502. Keep the public API contract: # no ready backend is a JSON 503 with the required tracing headers. location @no_ready_api { internal; default_type application/json; add_header X-Request-Id $request_id always; add_header X-Instance-Id nginx always; add_header Cache-Control "no-store" always; return 503 '{"error":{"code":"unavailable","message":"No ready API backend","request_id":"$request_id"}}'; } # Message POST is the only non-idempotent HTTP method that is safe to # replay here: the API requires Idempotency-Key and commits the message # together with its shared PostgreSQL idempotency record. location ~ ^/api/v1/chats/[0-9a-fA-F-]+/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_request_buffering on; proxy_connect_timeout 500ms; proxy_send_timeout 4s; proxy_read_timeout 4s; proxy_next_upstream error timeout http_502 http_503 http_504 non_idempotent; proxy_next_upstream_tries 2; } location ~ ^/api/v1/chats/[0-9a-fA-F-]+/attachments$ { 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_request_buffering on; proxy_connect_timeout 500ms; proxy_send_timeout 20s; proxy_read_timeout 20s; # Upload is not replayed: it has no idempotency key. The client may retry explicitly. proxy_next_upstream off; } 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 500ms; proxy_send_timeout 4s; proxy_read_timeout 4s; # No sticky sessions. Failed/unready peers are retried elsewhere. proxy_next_upstream error timeout http_502 http_503 http_504; proxy_next_upstream_tries 2; } } }