You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elastic Beanstalk部署Spring Boot API上传文件遇Nginx请求体过大错误

解决AWS Elastic Beanstalk上Spring Boot API文件上传请求体过大问题

问题分析

错误日志显示client intended to send too large body: 3527163 bytes(约3.36MB),说明当前Nginx的client_max_body_size限制小于该值。你尝试的两种配置未生效,核心原因是配置文件路径不符合平台要求或打包时未将配置目录纳入部署包根目录。

正确解决方案

1. 确认Elastic Beanstalk平台版本

先在EB控制台的环境配置页查看平台版本:

  • 若为Amazon Linux 2(当前主流版本),使用.platform目录配置
  • 若为Amazon Linux AMI(旧版),使用.ebextensions目录配置

2. 针对Amazon Linux 2的配置方法

方法A:使用.platform目录

  • 在项目根目录创建路径:.platform/nginx/conf.d/proxy.conf
  • 在proxy.conf中写入:
    client_max_body_size 50M;
    
  • 关键操作:确保.platform目录在部署包根目录
    若用Maven打包Spring Boot Jar,需在pom.xml中添加插件配置,将.platform复制到打包后的部署目录:
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <outputDirectory>${project.build.directory}/deploy</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-platform-config</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/deploy</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>.platform</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
    执行mvn clean package后,将target/deploy目录(包含Jar包和.platform)压缩为ZIP包上传到EB。

方法B:使用.ebextensions目录

  • 在项目根目录创建路径:.ebextensions/nginx/conf.d/00_client_max_body_size.conf
  • 在文件中写入:
    client_max_body_size 50M;
    
  • 同样需确保.ebextensions目录在部署包根目录,按上述Maven配置打包后上传。

3. 检查Spring Boot自身文件上传限制

即使Nginx配置生效,Spring Boot默认也有文件大小限制,需在application.properties(或application.yml)中添加:

# 单个文件最大允许大小
spring.servlet.multipart.max-file-size=50MB
# 整个请求体的最大允许大小
spring.servlet.multipart.max-request-size=50MB

4. 验证配置是否生效

部署完成后,登录EC2实例执行以下命令检查Nginx配置:

nginx -T | grep client_max_body_size

若输出包含client_max_body_size 50M;,说明配置已生效。

内容的提问来源于stack exchange,提问作者Podo Papson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 23:10:18