Elastic Beanstalk上传超57MB文件报405错误,本地正常
Hey there, let's tackle this 405 Method Not Allowed issue you're hitting with large file uploads on Elastic Beanstalk. I've run into similar quirks with EB and Nginx before, so here are targeted areas to investigate and fix:
Elastic Beanstalk often uses layered Nginx configs, so modifying the main nginx.conf might not be enough—your setting could get overwritten by application-specific configs.
- Fix: Create a custom Nginx config via
.ebextensionsto ensure the setting sticks. Make a file at.ebextensions/nginx/conf.d/large_uploads.confwith this content:
EB will automatically load this config during deployment, avoiding conflicts with default settings.client_max_body_size 5000M; - Check Logs: SSH into your EB instance and check
/var/log/nginx/error.logfor clues. Look for entries likerequest body is too large—this would confirm Nginx is still blocking large requests, even if you thought you updated the setting.
If your EB environment uses an Application Load Balancer (ALB), it has a default maximum request body size of 1MB—this is a common hidden limit that trips up large uploads.
- Console Fix: Go to your EB environment in the AWS Console → Configuration → Load Balancer → Edit the listener. Set the "Maximum request body size" to 5000MB (or larger).
- Code Fix: Add this to your
.ebextensionsYAML config to set it automatically:Resources: AWSEBV2LoadBalancerListener: Type: AWS::ElasticLoadBalancingV2::Listener Properties: LoadBalancerArn: Ref: AWSEBV2LoadBalancer Port: 80 Protocol: HTTP DefaultActions: - Type: forward TargetGroupArn: Ref: AWSEBV2TargetGroup MaxRequestBodySize: 5242880000 # 5000MB in bytes
A common gotcha: When Nginx hits a 413 Request Entity Too Large error, it might redirect to an error page using a GET request—but your client is still sending POST. This mismatch triggers a 405.
- Fix: Update your Nginx config to return the 413 status code directly instead of redirecting:
Create a simpleerror_page 413 =413 /413.html;413.htmlfile in your static assets folder to handle the error gracefully, or adjust the config to return a JSON response if needed.
Spring Boot's embedded Tomcat has a maxSwallowSize setting (default 2MB) that limits how much of a rejected request body it will process. If your upload exceeds this, Tomcat might close the connection abruptly, leading to Nginx returning a 405 instead of the actual 413 error.
- Fix: Add this to your
application.properties:server.tomcat.max-swallow-size=-1 # Disable the limit entirely
Since you're using Transfer Manager for multipart uploads, ensure your EB instance's IAM role has all the necessary S3 permissions beyond just s3:PutObject:
s3:InitiateMultipartUploads3:UploadParts3:CompleteMultipartUploads3:AbortMultipartUpload
If any of these are missing, the multipart upload could fail silently and manifest as a 405 due to connection issues.
Recommended Debugging Order
- Check Nginx error logs first to confirm the root cause.
- Verify ALB max request size is updated.
- Apply the custom Nginx config via
.ebextensions. - Add the Tomcat
max-swallow-sizesetting. - Double-check IAM permissions.
This should cover the most likely culprits for your 405 error with large uploads on EB. Let me know if you need to dive deeper into any of these steps!
内容的提问来源于stack exchange,提问作者Carsten

