Last active
October 20, 2025 10:38
-
-
Save snobu/7d0233b1153078a7af413064333bc84f to your computer and use it in GitHub Desktop.
nginx go around 429
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http { | |
| upstream azure_ai_foundry { | |
| zone azure_ai 64k; | |
| server foundry-endpoint-1.azure.com:443 max_fails=3 fail_timeout=60s; | |
| server foundry-endpoint-2.azure.com:443 max_fails=3 fail_timeout=60s; | |
| server foundry-endpoint-3.azure.com:443 max_fails=3 fail_timeout=60s; | |
| least_conn; | |
| keepalive 32; | |
| keepalive_requests 100; | |
| keepalive_timeout 60s; | |
| } | |
| server { | |
| listen 80; | |
| server_name your-domain.com; | |
| location / { | |
| proxy_pass https://azure_ai_foundry; | |
| proxy_http_version 1.1; | |
| # Preserve headers | |
| 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_set_header Connection ""; | |
| # SSL settings | |
| proxy_ssl_server_name on; | |
| proxy_ssl_protocols TLSv1.2 TLSv1.3; | |
| # Streaming configuration | |
| proxy_buffering off; # Disable response buffering | |
| proxy_cache off; # Disable caching | |
| proxy_read_timeout 300s; # Extended timeout for long streams | |
| proxy_send_timeout 300s; | |
| chunked_transfer_encoding on; # Enable chunked encoding | |
| # Disable X-Accel-Buffering for streaming | |
| proxy_set_header X-Accel-Buffering no; | |
| # Disable gzip for streaming responses | |
| gzip off; | |
| # Handle 429 and other errors | |
| # First attempt: Nginx sends the request to an Azure AI Foundry endpoint | |
| # If 429 received: Due to proxy_next_upstream http_429, nginx immediately tries | |
| # the second endpoint | |
| # Second attempt fails: Nginx tries the third endpoint | |
| # (still within proxy_next_upstream_tries 3) | |
| # If 90 seconds elapsed: Even if tries remain, nginx stops due to | |
| # proxy_next_upstream_timeout 90s and returns the error to the client | |
| proxy_next_upstream error timeout http_429 http_502 http_503 http_504; | |
| proxy_next_upstream_tries 3; | |
| proxy_next_upstream_timeout 90s; | |
| # Custom error handling | |
| proxy_intercept_errors on; | |
| error_page 429 = @rate_limit_error; | |
| } | |
| location @rate_limit_error { | |
| default_type application/json; | |
| return 429 '{"error": "Rate limit exceeded on all endpoints", "status": 429}'; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment