如何解决AWS ELB 60秒超时导致的504连接超时问题?
Hey there, I’ve dealt with this exact 504 timeout issue from ELB when handling long-running requests before—let’s walk through how to fix it by adjusting the idle timeout, since that’s the root cause here. The steps depend on which type of ELB you’re using, so I’ll cover all three common types:
Via AWS Console:
- Open the EC2 Console, navigate to Load Balancers in the left sidebar.
- Select your ALB, then switch to the Attributes tab.
- Look for the Idle timeout section, click Edit.
- Enter your desired timeout value (valid range: 1–4000 seconds) and save the changes.
Via AWS CLI:
Run this command, replacing <your-alb-arn> with your ALB's ARN and adjusting the Value to your needed timeout (example sets it to 5 minutes/300 seconds):
aws elbv2 modify-load-balancer-attributes --load-balancer-arn <your-alb-arn> --attributes Key=idle_timeout.timeout_seconds,Value=300
NLBs have a default idle timeout of 350 seconds, but you can extend it up to 4000 seconds:
Via AWS Console:
- Same starting steps: EC2 Console → Load Balancers → select your NLB.
- Go to the Attributes tab, find Idle timeout, click Edit.
- Input your target timeout value and save.
Via AWS CLI:
aws elbv2 modify-load-balancer-attributes --load-balancer-arn <your-nlb-arn> --attributes Key=idle_timeout.timeout_seconds,Value=600
(The example above sets it to 10 minutes—tweak the Value as needed.)
CLBs default to a 60-second idle timeout, which is likely why you’re hitting that 504 error. Here’s how to adjust:
Via AWS Console:
- EC2 Console → Load Balancers → select your CLB.
- Switch to the Description tab, locate Connection Settings and click Edit.
- Update the Idle timeout (valid range: 1–3600 seconds) and save.
Via AWS CLI:
Replace <your-clb-name> and set the IdleTimeout value to your requirement (example uses 2 minutes/120 seconds):
aws elb modify-load-balancer-attributes --load-balancer-name <your-clb-name> --load-balancer-attributes "{\"ConnectionSettings\":{\"IdleTimeout\":120}}"
Critical Notes to Avoid Future Issues:
- Don’t forget your backend services! ELB’s timeout is only one piece—you need to adjust timeouts on your backend instances too. For example:
- Web servers (Nginx/Apache) request timeouts
- Application server (Tomcat/Node.js) connection timeouts
- Database query timeouts if your request relies on long-running DB operations
- If you’re using other AWS services in the chain (like API Gateway or Lambda), make sure their timeout settings align with your ELB’s new value. Mismatched timeouts will still cause failures.
- Keep the timeout value reasonable—setting it to the maximum 4000 seconds isn’t always ideal, as it can tie up connection resources unnecessarily. Stick to the minimum value that works for your longest-running requests.
内容的提问来源于stack exchange,提问作者javaLearner java




