events { worker_connections 1024; } http { access_log /dev/stdout; error_log /dev/stderr warn; upstream api_backend { server api1:8080 max_fails=1 fail_timeout=5s; server api2:8080 max_fails=1 fail_timeout=5s; } server { listen 8080; 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 Connection ""; # Retry the other replica on a transport failure or a 5xx from a dying backend. proxy_next_upstream error timeout http_502 http_503 http_504; proxy_connect_timeout 2s; proxy_send_timeout 10s; proxy_read_timeout 10s; } } }