NGINX – Set client_max_body_size for One Route on AWS Elastic Beanstalk

amazon-web-serviceselastic-beanstalknginxnode.js

I have a Node.Js application deployed to Amazon Elastic Beanstalk. When uploading a file, I get the following error:

2019/03/04 17:00:42 [error] 4325#0: *722 client intended to send too large body: 30877841 bytes,
client: my ip,
server: ,
request: "POST /api/files/upload HTTP/1.1",
host: "mydomain.com",
referrer: "mydomain.com/..."

Therefore, I need to add client_max_body_size XXM to nginx.conf.

Issues

(1) But as I am using Amazon Elastic Beanstalk, I don't know how to do this, without connecting to every single EC2 instance and updating the file manually.

(2) I also want to increase the max size only for one url.

Best Answer

You can customize the software on a Linux Elastic Beanstalk instance using environment configuration files. For your specific case, in your local application home folder, you should have a .ebextensions directory. In that folder create a new file, let's call it "nginx.config", and place the following into that file:

---
files:
  /etc/nginx/conf.d/00_client_max_body_size.conf:
    content: "client_max_body_size 10m;"
    group: root
    mode: "000644"
    owner: root

This instructs Elastic Beanstalk to create a file in /etc/nginx/conf.d/ called 00_client_max_body_size.conf, and to place a single line in this file containing:

client_max_body_size 10m;

Bundle up your application and upload a new version to Elastic Beanstalk. Once you have done this, you will notice the following on your Linux instance:

$ ls -al     /etc/nginx/conf.d
total 20
drwxr-xr-x 2 root root 4096 Mar  8 00:34 .
drwxr-xr-x 4 root root 4096 Feb 15 22:24 ..
-rw-r--r-- 1 root root   25 Mar  8 00:34 00_client_max_body_size.conf
-rw-r--r-- 1 root root 1351 Mar  8 00:35 00_elastic_beanstalk_proxy.conf
-rw-r--r-- 1 root root  283 Dec 13 00:21 virtual.conf

This will get included by the main nginx.conf file that is present on all nodejs elastic beanstalk environments, as this file has a line at the end of its http block as follows:

http {

...

include /etc/nginx/conf.d/*.conf;
# End Modification
}
Related Topic