DEV Community

Cover image for Elastic Beanstalk - Update Media Upload Size
Rasool Khan
Rasool Khan

Posted on

Elastic Beanstalk - Update Media Upload Size

Introduction

Elastic Beanstalk is an AWS service for deploying and scaling web applications. We can upload our project code and elastic beanstalk will automatically handle the deployment, load balancing, auto-scaling, CloudWatch, etc.

Media Upload in S3

In a local environment, when we upload a media file to an AWS S3 bucket, the file is first uploaded to our local server and then from our application the file is uploaded to S3 bucket.

The Problem

But in production mostly we use Nginx web server. So, the Elastic Beanstalk basically creates an EC2 instance and uses Nginx in front of the node server as a proxy server.

So, the problem arises here because Nginx server is a proxy server it doesn't allow unlimited upload size. Nginx has a default upload size as 1 MB for all file uploads. That's why when we upload a file which is over 1 MB size to S3 using an application deployed on Elastic Beanstalk it throws an error:

413 – Request Entity Too Large

To solve this issue we have to add a custom Nginx configuration file.

Solution

Elastic Beanstalk provides a number of platforms(docker, Go, Node, Java, etc.) which can be used to build our application. The Nginx is installed on the platform so to add a custom Nginx configuration file we need to add nginx.conf file inside of the platform folder.

The below example shows how the our app is structured in the Elastic Beanstalk. You can read more about it here.

~/my-app/
|-- web.jar
|-- Procfile
|-- readme.md
|-- .ebextensions/
|   |-- options.config        # Option settings
|   `-- cloudwatch.config     # Other .ebextensions sections, for example files and container commands
`-- .platform/
    |-- nginx/                # Proxy configuration
    |   |-- nginx.conf
    |   `-- conf.d/
    |       `-- custom.conf
Enter fullscreen mode Exit fullscreen mode

This is the folder structure we need to follow in order to add custom Nginx config file. Create a .platform folder in your project's root directory. Then create a file called proxy.conf.

Folder Structure

The proxy.conf file will be placed at ".platform/nginx/conf.d/proxy.conf". The contents of this file will be:

client_max_body_size 100M;
Enter fullscreen mode Exit fullscreen mode

After adding this file, deploy your app to Elastic Beanstalk. Now, we will be able to upload any file up to size 100MB.

Note: When deploy our app, Nginx server will automatically reload. If you are still not able to upload above 1MB then, the issue could be that Nginx server was not reloaded properly. To solve this, we can add another file. In the root of .platform folder create a file called as myconf.config. This command will basically reload our Nginx server before starting. The contents of this will be as follows:

container_commands:
  01_reload_nginx:
    command: "service nginx reload"
Enter fullscreen mode Exit fullscreen mode

Note: If you are using docker then add the .platform folder in the root directory along with Dockerrun.aws.json. Then bundle them as zip and deploy.

Summary

We saw how Nginx server only allows 1MB of file upload when deployed in Elastic Beanstalk. And we saw how to solve this by creating a custom Nginx configuration file.

Please like and share if you found it useful. And follow me on twitter.

Thank you for reading!

Discussion (0)