ChatGPT解决这个技术问题 Extra ChatGPT

Increasing client_max_body_size in Nginx conf on AWS Elastic Beanstalk

I'm running into "413 Request Entity Too Large" errors when posting files larger than 10MB to our API running on AWS Elastic Beanstalk.

I've done quite a bit of research and believe that I need to up the client_max_body_size for Nginx, however I cannot seem to find any documentation on how to do this using Elastic Beanstalk. My guess is that it needs to be modified using an ebetension file.

Anyone have thoughts on how I can up the limit? 10MB is pretty weak, there has to be a way to up this manually.

The default is only 1MB these days...
For the record all of the provided solutions do not tackle .NET deployments. If you use .NET in AWS Elastic Beanstalk you must configure the IIS settings in your project. For me I had to configure the web.config in my net471 app.

r
rogerdpack

There are two methods you can take for this. Unfortunately some work for some EB application types and some work for others.

Supported/recommended in AWS documentation

For some application types, like Java SE, Go, Node.js, and maybe Ruby (it's not documented for Ruby, but all the other Nginx platforms seem to support this), Elasticbeanstalk has a built-in understanding of how to configure Nginx.

To extend Elastic Beanstalk's default nginx configuration, add .conf configuration files to a folder named .ebextensions/nginx/conf.d/ in your application source bundle. Elastic Beanstalk's nginx configuration includes .conf files in this folder automatically.

~/workspace/my-app/

|-- .ebextensions
|   `-- nginx
|       `-- conf.d
|           `-- myconf.conf
`-- web.jar

Configuring the Reverse Proxy - Java SE

To increase the maximum upload size specifically, then create a file at .ebextensions/nginx/conf.d/proxy.conf setting the max body size to whatever size you would prefer:

client_max_body_size 50M;

Create the Nginx config file directly

For some other application types, after much research and hours of working with the wonderful AWS support team, I created a config file inside of .ebextensions to supplement the nginx config. This change allowed for a larger post body size.

Inside of the .ebextensions directory, I created a file called 01_files.config with the following contents:

files:
    "/etc/nginx/conf.d/proxy.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
           client_max_body_size 20M;

This generates a proxy.conf file inside of the /etc/nginx/conf.d directory. The proxy.conf file simply contains the one liner client_max_body_size 20M; which does the trick.

Note that for some platforms, this file will be created during the deploy, but then removed in a later deployment phase.

You can specify other directives which are outlined in Nginx documentation.

http://wiki.nginx.org/Configuration

Hope this helps others!


The file format is documented at docs.aws.amazon.com/elasticbeanstalk/latest/dg/…. The progress is logged at /var/log/cfn-init.log. In the logs you should see something like 2014-xx-xx xx:xx:xx,xxx [DEBUG] Writing content to /etc/nginx/conf.d/proxy.conf. I'm not sure, but it seemed like restarting the server might be necessary.
Working for me with puma. Had to restart service like @Will said though (sudo service nginx reload).
For node js, the second method (files: ) worked for me. The first one did not work. Even AWS support advised to use the second method for node js docs.aws.amazon.com/elasticbeanstalk/latest/dg/…
I have just tested both options for ElasticBeanstalk + Java8 environment and the first one worked for me. The second one didn't. Hope it helps!.
For anyone looking, the correct Nginx conf path for Python on Elastic Beanstalk Amazon Linux 2/3.3.1 is .platform/nginx/conf.d/proxy.conf
J
Jijo Cleetus

I have tried all .ebextensions method of adding implementation level configuration and it didn't helped me in the latest Amazon Linux AMI. I have did a lot research and after going through the logs i can find the deployment task runner is checking for a folder called .platform everytime and i thought of add one just like the .ebextensions. Below is the settings i have done in my root folder of my project.

Add the below folder setup in the root level of your project folder.

Folder structure (.platform/nginx/conf.d/proxy.conf)

.platform/
         nginx/
              conf.d/
                    proxy.conf
         00_myconf.config

Content of File 1 - proxy.conf (Inside .platform/nginx/conf.d/ folder)

client_max_body_size 50M;

Content of File 2 - 00_myconf.config (Inside .platform/ folder)

container_commands:
  01_reload_nginx:
    command: "service nginx reload"

Care full with the extensions. First file is .conf and second file is .config.

Now redeploy your project to Amazon Elastic Beanstalk and you will see the magic. This configuration will be added to all your EC2 instances, created as part of auto scaling.

Detailed folder structure below.

https://i.stack.imgur.com/aNnIW.jpg


Man!!!!!!!! Thank you so much. This is the only way to set nginx config in AWS EB Amazon linux. I wasted a whole day trying every available solution!!! I cant express how to to thank you for taking the time to write this answer in detail. Bless you. If you have some kind of patreon or buymeacoffee account Il contribute. @jijo Cleetus
Thanks a lot. I too tried all possible approaches using .ebextensions and only this approach worked.
Saved me tons of trials and failures getting this to work. Can't thank you enough !
Has anyone found any reference to this in the AWS docs?
Nice work. This is documented by AWS but very poorly. docs.aws.amazon.com/elasticbeanstalk/latest/dg/…. See Reverse proxy configuration section. "All Amazon Linux 2 platform versions use nginx as their default reverse proxy server....To extend the Elastic Beanstalk default nginx configuration, add .conf configuration files to a folder named .platform/nginx/conf.d/ in your application source bundle"
J
JanDintel

When using Amazon Linux 2 (regardless of platform)

The accepted answer is correct when you're using an older Elastic Beanstalk environment using Amazon Linux AMI. Newer Elastic Beanstalk environments use the Amazon Linux 2 AMI.

When using Amazon Linux 2 you need to specify the custom Nginx config in the .platform/ directory instead of the .ebextensions/.

This means that you'll need to create the file .platform/nginx/conf.d/proxy.conf with the content client_max_body_size 50M; instead.

The .platform/ directory was also mentioned in another answer, but that still required reloading the Ngix config, with Amazon Linux 2 reloading is no longer needed.

You can find more options about this in the Extending Elastic Beanstalk Linux platforms documentation.


Worked for Amazon Linux 2 and Node.js!
Remember to include .platform/* alongside the rest of your source files in your source bundle.
important takeaway by @Kieran, in my example with Java Spring Boot i had to create the zip of the jar and .platform/* folder together on the same level
@JanDintel the other .platform answer and your are kinda not aligned on reloading the Nginx config. So at the end the reloading of the config is not needed and is done by AWS?
@AleksandarT it's no longer needed to add an after hook to reload the Nginx config when using this method
L
Lenin Raj Rajasekaran
files:
    "/etc/nginx/conf.d/proxy.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
           client_max_body_size 20M;

Modified the above answer for the sake of security (and the syntax was wrong, see, two 'owner:' entries in the YAML), guys, please don't set 777 permissions on ANYTHING. Unless you enjoy being hacked, and set the owner of Nginx config files to root.

Also see the below answer to make nginx pickup this change after deployment.


W
Will

EDIT: After you've deployed a build with the instructions in the accepted answer by Nick Parsons, you may need to restart the nginx server to pick up the changes.

To do this, ssh to the instance and do

sudo service nginx reload

To learn more about reloading, see http://nginx.org/en/docs/beginners_guide.html.

In a previous version of Elastic Beanstalk, I was able to add a container_command to accomplish this, but now I am finding, like @cdmckay, that this causes a deployment failure. If you rebuild your environment it will pick up the client_max_body_size settings as well as long as that instruction is in your config file.


Is this necessary? If you don't add this, how will it restart?
In my experience I found this necessary.
@cdmckay can you say more about the instance issues the reload command caused?
I didn't look into it too closely, but it basically prevented my server from launching until I removed it. It doesn't appear to be necessary.
in my experience i found this was necessary however, I had to add a check to see if nginx was running before trying to restart it for new instances - 'pgrep nginx && service nginx reload || true'
D
Daniel Compton

The accepted answer didn't work for me since I have a JVM-based app and it seems to do NGINX configuration differently. I would see a proxy.conf file being created during the deploy but then later deleted before the deploy was completed. AWS documentation explains how to configure the proxy:

Create an .ebextensions/nginx/conf.d/proxy.conf file that contains just the line: client_max_body_size 40M;


Unfortunately, that didn't work for me (single-container Docker ELB) - not even after a reboot of the instance. Logging in to the instance via SSH there is no proxy.conf file being created in /etc/nginx/conf.d/
I'm assuming it's because of the fact you're using a Docker ELB and it does nginx conf differently. My answer is for Java-preset ELB.
I am using elb and java app. Works for me! Thanks!
How can you see the proxy.conf file being created, just noting its temporary presence?
this works for Amazon Linux 1 (legacy instances only), for Amazon Linux 2, change ebextensions to .platform
S
Shashank Agrawal

Super Simple Solution for Amazon Linux 2:

On the same level as .ebextensions and .elasticbeanstalk, create a directory called .platform Create a file (full path shown) under this directory called: .platform/nginx/conf.d/client_max_body_size.conf Add this one line to the client_max_body_size.conf file: client_max_body_size 20M; Deploy!!

This is based on AWS NGINX "Reverse proxy configuration" Documentation.

(This worked w/ Django 3.1.4 + Python 3.7 running on 64bit Amazon Linux 2/3.2.0)


works with NodeJS running on Amazon Linux 2 too!
this doesn't seem to work for me... i've also tried the same directory inside .ebextensions
C
Conor

Following on from the accepted answer, you need may need to reload the nginx config file.

In order to do this add the following command

   container_commands:
      01_reload_nginx:
        command: "service nginx reload"

This would be better practice than ssh'ing into your eb instance and manually doing it with a command.

This combined with the accepted answer solved the same issue for me. (Rails, Puma, NGINX)


k
kkyr

The accepted answer did not work for me, so instead I overrode the nginx configuration with my own.

I created a file called nginx.conf under the directory .ebextensions/nginx/

I SSHed into a running instance of my Beanstalk app, and copied the contents of the nginx.conf file, using cat /etc/nginx/nginx.conf and copying from the terminal.

I pasted the contents into the nginx.conf file I previously created in .ebextensions/nginx/, and modified the http directive to include client_max_body_size 50M;. I finally redeployed my app using eb deploy and it worked. You should get the following message during deployment:

INFO: Nginx configuration detected in the '.ebextensions/nginx' directory. AWS Elastic Beanstalk will no longer manage the Nginx configuration for this environment.

These are the contents of my .ebextensions/nginx/nginx.conf file:

# Elastic Beanstalk Nginx Configuration File

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    33193;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    include       conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default     "upgrade";
    }

    server {
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }

    client_max_body_size 50M;
}

I did not have to restart the nginx service nor the environment.

Note: Make sure your .ebextensions is part of the .zip file created and uploaded to Beanstalk during deployment (it's not ignored in .gitignore or .ebignore if you're using it).


Thanks for this! I was wondering which platform you were using though? For some reason Beanstalk isn't detecting my ` .ebextensions/nginx/nginx.conf` file even though it's in the zip file and git repo.
Upvoted for the note. I was ignoring the .ebextensions which caused the accepted and popular answer to fail. I did not try everything else you mentionned, but thanks for the note 🍻
Can it be used for Node.js running on 64bit Amazon Linux?
E
Ed Vieira

The only thing that worked for me was to create a ".config" file inside .ebextensions like this:

.ebextensions/
           proxy.config

with only this content in the .config file:

files:
  "/etc/nginx/conf.d/proxy.conf":
     content: |
       client_max_body_size 50M;

no need for subfolders, no need to restart the application server, pay attention that is a ".config" not a ".conf" file inside .ebextensions and the use of proper indentation to avoid errors in the aws console the rest is the same doesn't matter the name of the file,

thanks to : http://kroltech.com/2014/09/14/quick-tip-increase-upload-size-in-aws-elastic-beanstalk-node-js-env/


T
Tony BenBrahim

This is the AWS provided solution, and it works (adjust the size to your needs)

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/enact/12_add_nginx_configuration.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      /bin/echo "client_max_body_size 50M;" > /etc/nginx/conf.d/proxy.conf
      /sbin/service nginx reload

Y
Yanir Calisar

After 3 long days of trying to figure this one out, I jumped on a call with the amazing AWS support team and they gave me some clues about how to solve it. First, my project is in JAVA and I use maven and spring boot to run it through Elastic Beanstalk (EBS).

As explained on AWS' documentation, you need to have the custom nginx settings stored in the root level of your project. To do that I have created a client_max_body_size.conf file and placed it in the following path: myprojectname/src/main/resources/ebextensions/nginx/conf.d/client_max_body_size.conf This file contains only the following line: client_max_body_size 10M; Configure maven to add this file in the root folder of my project during the build. This was a bit tricky, I needed to add the following configuration (source) in my POM.xml: maven-resources-plugin 3.1.0 copy-resources validate copy-resources ${basedir}/target/.ebextensions src/main/resources/ebextensions true You can now already build your project locally and run the following SSH command to validate it's actually located in the root of your project: jar tf main-1.0-SNAPSHOT.jar | grep .ebextensions Now it's the tricky part. When you upload it to EBS, your file should contain only the SNAPSHOT.jar file and the .ebextensions folder. For example, if now you will zip the jar file and the folder and upload it manually, it will work! Since I am using Jenkins for my deployment, and specifically the AWS EBS Deployment Plugin - you need to change the settings of the files/folder you include in the deployment. Now for some reason I wasn't able to include the .ebextensions folder so I just excluded everything else except the folder and the .jar file.

This works!


A
Aerodynamika

If you are running EC2 and installed nginx yourself, your best solution is to create a new file in

/etc/nginx/conf.d

folder:

sudo nano /etc/nginx/conf.d/proxy.conf

and then add the following line there:

client_max_body_size 20M;

then save and restart nginx:

sudo systemctl restart nginx

This is a work around but incase Elastic Beanstalk creates a new instances, you need to give this configuration in each instances. And it is practically not possible since we need to constantly monitor the EC2 intances and nginx restart process is not good everytime
Thanks this worked for me. Found your solution after 2 hours of headache.
A
Anubhab Maji

For those who are stumbling on here:

I followed Jijo Cleetus answer but did not work, but definitely pointed to the right direction.

But I am wordering why because if I see nginx.conf file, it does contain include conf.d/*.conf so it should include the proxy file as the answer mentioned.

Also the default server listening on 80 and marked as default_server, has include conf.d/elasticbeanstalk/*.conf

But this answer did solve the issue, in gist, the .conf files directory have to be .platform/nginx/conf.d/elasticbeanstalk instead of .platform/nginx/conf.d/.

So:

.platform/nginx/conf.d/elasticbeanstalk/01-request-body.conf .platform/nginx/conf.d/elasticbeanstalk/02-timeouts.conf

are some examples

I also confirmed it by logging into the ec2 instance and running nginx -T which prints the running config to verify if my custom configs were included.

Also there I saw, the default application proxy config is named 00_application.conf and nginx includes files in alphabetical order, so named it by prefixing numbers.

Added notes to those who are deploying via CodePipeline: Make sure to include .platform/**/* in buildspec.yaml -> artifacts section


Thank you, this allowed me to replace the stock 00_application.conf that Elastic Beanstalk uses for an Nginx forward proxy for ASP.NET Core web apps. I had to also add the file to the ASP.NET Core project in Visual Studio as well otherwise it's not published (make sure you see it included in the bin/Release folder and also in the deployed zip uploaded to S3).
I was deploying using code pipeline and your awesome guidance of adding the artifacts for .platform solved the issue.
p
pascal

For Golang without Docker I followed these instructions from aws doc:

Configuring the Reverse Proxy If you want to include directives in addition to those in the nginx.conf http block, you can also provide additional configuration files in the .ebextensions/nginx/conf.d/ directory of your source bundle. All files in this directory must have the .conf extension. http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/go-environment.html#go-complex-apps

I created the file proxy.conf in .ebextensions/nginx/conf.d/ at the root of my project, with simply 1 line inside:

client_max_body_size 20M;

If it still doesn't work, make sure .ebextensions folder and sub-folders are included in your deployment zip. No need to restart Nginx manually.


This worked for me running a Java 8 App with Amazon Linux instances (Version 1)
h
harshal jadhav

I was struggling with the same issue but wasn't able the resolve it, finally its working.

here is my congfig file,

files:
  "/etc/nginx/conf.d/01_proxy.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      client_max_body_size 50M;
      client_body_buffer_size 16k;

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

The issue was,

I was using a python script for code pipeline which does not include the .ebextensions folder while creating the build.

Here are my few cents,

make sure that your .ebextensions folder is included in your build's zip folder which is located in s3bucket of your aws account make sure it is not in the .gitignore file if you are using a script to generate the build make sure it includes .ebextensions


p
peter.swallow

Any of the above options may or may not work,

...but if you're line endings of the proxy.config file are not correct then AWS will just error when trying to deploy your code to the instance.

We created the .platform/nginx/conf.d/proxy.config file, but because we are developing and building the code on a windows platform, the line endings were set to Windows (CRLF).

But our AWS instances were all Linux, which expect the Unix (LF) ending.

So when Linux tried to read the file it just failed...it didn't even give us a decent error in the events or logs, it just said failed.

Maybe a last thing to check if nothing else works :)


D
David Smits

Alternatively you could change the proxy server to Apache. To do this, go to the Configuration and Edit the Software Configuration. The first option here is “Proxy server”, select “apache”.


Apache has it's own LimitRequestBody directive to set the max size of uploaded files.
@TomHarvey If available, would you kindly provide a full example for the use case with LimitRequestBody?
S
Shaun

Late to the party but for .NET Core the following solution works:

Make a file in /.platform/nginx/conf.d/proxy.conf with the content:

client_max_body_size 100M;

Make sure in properties on Visual Studio that Copy to output directory is set to "Always" for this file and that the build action is "content."


L
Lyudmyla

For Java Platform

To create the NGINX config proxy file you should just add

.ebextension/nginx/conf.d/proxy.conf file

with the content client_max_body_size 20M; in it.

"proxy.conf" will be deployed to "/etc/nginx/conf.d/proxy.conf" and automatically included by the NGINX config.


N
Norm

In addition to client_max_body_size, I had to add client_body_buffer_size. Here is the dot config file that worked, for a 2 MB attachment:

files: "/etc/nginx/conf.d/proxy.conf" : mode: "000755" owner: root group: root content: | proxy_buffering on; proxy_buffer_size 128k; proxy_buffers 8 256k; client_body_buffer_size 2M; proxy_busy_buffers_size 256k; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; client_max_body_size 3M;


J
JasonZ

Creating a file in the .ebextensions directly is the only method I found that has worked for me. Create a new file called 01_files.config and paste in the following:

files:
"/etc/nginx/conf.d/proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
       client_max_body_size 20M;

This will create a config file for you on deploy that sets the client_max_body_size to 20MB (change the # to suit your needs).


m
moccasine

Had a lot of trouble with this even trying all of the solutions here. I got the error message: unknown directive "client_max_body_size" in the eb-engine.log

This page help me solve the issue: https://forums.aws.amazon.com/thread.jspa?threadID=334637

Visual Studio had create the file in some encoding that caused the problem and I could save/convert the file to ANSI encoding and it worked as expected.

Hope this can help someone stuck with the same problem.


L
Locutus

For Spring Boot / Java / Fat-Jar deployments on AWS Beanstalk:

The previous answer using .platform is required in AWS Linux 2, but, unlike .ebextensions, you can't include the .platform directory inside the fat jar - it must be outside the Java jar.

This useful post describes how to set up Maven to create an AWS fat jar for deployment.

https://www.coderdreams.com/deploying-spring-boot-app-to-aws-beanstalk-with-nginx-customization/

Note: there's no need to create a script to restart the nginx server as in the previous answer.


The linked answer includes so extra steps that are superfluous see my comment on the article.
A
AkshayKrison

Create Auto configuration for Nginx in EBS

Since the EBS is auto scaling we can’t increase the Size and request time out manually so we have to add conf file in you source code which automatically include in the nginx configuration in EBS

https://akshaykrisonz.medium.com/fix-413-request-entity-too-large-nginx-error-on-aws-elastic-beanstalk-via-ebs-configuration-7533ad28ac06


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now