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.
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.
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.
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
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 file will be placed at ".platform/nginx/conf.d/proxy.conf". The contents of this file will be:
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
.platformfolder 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
.platformfolder in the root directory along with
Dockerrun.aws.json. Then bundle them as zip and deploy.
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!