Increasing client_max_body_size in Nginx conf on A

2019-01-04 17:43发布

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.

9条回答
三岁会撩人
2楼-- · 2019-01-04 17:49

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.

查看更多
在下西门庆
3楼-- · 2019-01-04 17:51

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;

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-04 17:59

There are two methods you can take for this:

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

After much research and hours of working with the wonderful AWS support team, I created a config file inside of .ebextensions to modify 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!

查看更多
可以哭但决不认输i
5楼-- · 2019-01-04 17:59

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”.

查看更多
神经病院院长
6楼-- · 2019-01-04 18:02

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).

查看更多
虎瘦雄心在
7楼-- · 2019-01-04 18:09

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.

查看更多
登录 后发表回答