Introduction When the origin server takes too long to respond, the CDN returns a 504 Gateway Timeout. This is common for dynamic content, API responses, or overloaded origin servers.

Symptoms - CDN returns 504 Gateway Timeout - Error: "The origin server did not respond in time" - Specific endpoints timing out while others work - Timeout correlates with origin server load spikes - CDN logs show "OriginResponseTimeout"

Common Causes - Origin server overloaded or slow - CDN timeout too short for specific endpoints - Database query slow for dynamic content - Origin not returning response headers quickly - Network latency between CDN and origin

Step-by-Step Fix 1. **Check origin response time': ```bash curl -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" \ -o /dev/null https://origin.example.com/slow-endpoint ```

  1. 1.**Increase CDN origin timeout':
  2. 2.```bash
  3. 3.# CloudFront
  4. 4.aws cloudfront update-distribution --id E123 --cli-input-json '{
  5. 5."DistributionConfig": {
  6. 6."Origins": {"Items": [{"Id": "origin", "DomainName": "origin.example.com",
  7. 7."CustomOriginConfig": {"OriginReadTimeout": 30}}], "Quantity": 1}
  8. 8.}
  9. 9.}'
  10. 10.`
  11. 11.**Add origin shield to reduce origin load':
  12. 12.Enable CDN origin shielding to cache responses closer to origin.

Prevention - Monitor origin response time with alerts - Cache dynamic content with short TTLs - Implement origin response optimization - Use CDN edge computing for dynamic content - Set origin timeout based on p99 response time