Sun Dec 29 2024

How to Increase client_max_body_size for Nginx on AWS Elastic Beanstalk

Running into a “413 Request Entity Too Large” error while trying to upload files greater than 10MB can be frustrating, especially when you’re running your application on AWS Elastic Beanstalk. This error signifies that Nginx, the reverse proxy server often used with Elastic Beanstalk, needs its client_max_body_size directive configured to allow for larger files.

AWS Elastic Beanstalk can host various application types with Nginx, each having different approaches to increasing the upload limit. Here’s how you can modify the Nginx configuration on Elastic Beanstalk to resolve this issue.

Configuring Nginx via .ebextensions

AWS Elastic Beanstalk provides a convenient method for certain platforms like Java SE, Go, Node.js, and potentially Ruby to leverage Nginx configuration. You can do this by extending Elastic Beanstalk’s default Nginx settings using the .ebextensions directory.

  1. Create the .ebextensions Structure: In your application source bundle, create a directory structure as follows:

    ~/workspace/my-app/
    |-- .ebextensions
    |   `-- nginx
    |       `-- conf.d
    |           `-- proxy.conf
    
  2. Add Nginx Directives: Inside the proxy.conf file within the conf.d directory, add the configuration to set a maximum body size:

    client_max_body_size 50M;
    

    This snippet increases the maximum body size for uploads to 50MB. Adjust this value according to your application’s requirements.

  3. Deploy Your Application: Once your .ebextensions folder is configured, include it in your application package and redeploy your Elastic Beanstalk environment. During deployment, Elastic Beanstalk will automatically apply these configurations to Nginx.

Alternative: Manually Create Nginx Config Files

If the above method doesn’t work for your platform or if it’s being overridden, you can directly create a configuration file within the Elastic Beanstalk instance by utilizing a config file in .ebextensions.

  1. Create 01_files.config: Add a file named 01_files.config within the .ebextensions directory of your application. The content should look like this:

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

    This method writes a proxy.conf file directly into the Nginx configuration directory, setting the maximum body size to 20MB or your preferred size.

  2. Deploy Your Application: Package this configuration with your application and deploy it. This file will generate during deployment, ensuring that your specified client_max_body_size setting is applied.

Note: On some platforms, temporary files created during deployment might be removed later. Ensure that the config file persists throughout the deployment by testing thoroughly and making modifications as needed.

For more detailed configurations and options, consult the Nginx official documentation.

Adjusting the client_max_body_size directive allows your application running on AWS Elastic Beanstalk to handle larger file uploads seamlessly. By following these instructions, you can ensure that your application meets user demand without hiccups.