How do I customize my nginx configuration to modify the "client_max_body_size" in Elastic Beanstalk?

2 minute read
1

I want to upload large size files to my AWS Elastic Beanstalk environment without receiving the error message "413 Request Entity Too Large".

Short description

By default, NGINX has a limit of 1MB on file uploads. If the size of a request exceeds the configured value, then the 413 Request Entity Too Large error is returned. To upload files larger than 1 MB, configure the client_max_body_size directive in the NGINX configuration files.

Important: M and MB are equivalent expressions for "megabyte". For example, 2M is equal to 2 MB. However, use only M in your configuration file, as MB isn't valid in a configuration file.

Resolution

To configure the client_max_body_size in Amazon Linux 2 environments, do the following:

1.    To extend the Elastic Beanstalk default NGINX configuration, add the .conf configuration file client_max_body_size.conf that includes the following:

client_max_body_size 50M;

Note: In the preceding example, the value of the client_max_body_size is updated to 50M. Substitute any value in place of 50 as per your requirements.

2.    Copy the .conf configuration file client_max_body_size.conf to a folder named .platform/nginx/conf.d/ in your application source bundle. The Elastic Beanstalk NGINX configuration includes .conf files in this folder automatically. Make sure to create this path if it doesn't exist in your source bundle. The following example shows the structure of the .platform directory and .conf file in the application zip file:

-- .ebextensions
       -- other non nginx server config files
            
-- .platform
       -- nginx
           -- conf.d
                 -- client_max_body_size.conf
                   
-- other application files

The file client_max_body_size.conf has a path like this: my-app/.platform/nginx/conf.d/client_max_body_size.conf.

3.    Deploy your code and the new .platform/ directory together as a new application version in your Elastic Beanstalk environment.

4.    After the deployment is completed, log in to the instance running on the Elastic Beanstalk environment. After logging in, check that the settings to the NGINX server are applied. To do this, use the following command:

$ sudo nginx -T | egrep -i "client_max_body_size"
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
client_max_body_size 50M;

AWS OFFICIAL
AWS OFFICIALUpdated 5 months ago
3 Comments

We've been researching for days, but everything else on the net is outdated - this has worked now!

Step 3 had to be implemented specifically for our application. In our Spring Boot Java 8 application, the following snippet had to be added to the pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>prepare</id>
            <phase>package</phase>
            <configuration>
                <tasks>
                    <copy todir="${project.build.directory}/${project.build.finalName}/" overwrite="false">
                        <fileset dir="./" includes=".platform/**"/>
                        <fileset dir="${project.build.directory}" includes="*.jar"/>
                    </copy>
                    <zip destfile="${project.build.directory}/${project.build.finalName}.zip"
                         basedir="${project.build.directory}/${project.build.finalName}"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
profile picture
replied a month ago

Hi there! I've been the same journey for days and tried all the above procedures without success. As you can see attached I have the same structure in my project with the file and also included this in the POM.xml

 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>prepare</id>
            <phase>package</phase>
            <configuration>
              <tasks>
                <copy todir="${project.build.directory}/${project.build.finalName}/"
                  overwrite="false">
                  <fileset dir="./" includes=".platform/**"/>
                  <fileset dir="${project.build.directory}" includes="*.jar"/>
                </copy>
                <zip destfile="${project.build.directory}/${project.build.finalName}.zip"
                  basedir="${project.build.directory}/${project.build.finalName}"/>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

But then, checking in the instance that's all I see:

[ec2-user@ip-XXXXXXX /]$ sudo nginx -T | egrep -i "client_max_body_size"
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Can you guys help me?

replied 12 days ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 12 days ago