Elastic Beanstalk - Update Media Upload Size
Tags: nginx, elasticbeanstalk, s3, cloud
Published: 2021-11-11 10:58:20

## 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](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html).

```
~/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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf2oa8rv8fj8ywi3v0v2.png)

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;
```

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"
```

>
**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](https://twitter.com/Dotnetdotnet). 

Thank you for reading!