Nginx与Amazon ElasticBeanstalk中413请求实体过大问题未解决
I’ve run into the exact same issue with older AL1-based EB environments—your initial config makes total sense, but Elastic Beanstalk’s default Nginx setup often overrides custom proxy.conf files without you noticing. Here’s how to get it working:
Why Your Initial Config Failed
Amazon Linux 2018.03 EB environments load Nginx configurations in this order:
/etc/nginx/conf.d/*.conf(your originalproxy.conflives here)/etc/nginx/conf.d/elasticbeanstalk/*.conf(EB’s auto-generated configs, including00_application.conf)
EB’s own configs typically include a small default client_max_body_size (like 1M) that loads after your proxy.conf, overwriting your 50M setting entirely.
Solution 1: Use a High-Priority Config File
Create a config file with a name that loads after EB’s default files (using a 99_ prefix ensures this). Add this to a .ebextensions/nginx_max_upload.config file in your Rails project root:
files: "/etc/nginx/conf.d/99_my_proxy.conf": mode: "000755" owner: root group: root content: | client_max_body_size 50M; container_commands: 01_reload_nginx: command: "service nginx reload"
The late-loading prefix guarantees your setting takes precedence over any earlier defaults.
Solution 2: Modify EB’s Auto-Generated Config
If you prefer to edit EB’s own config directly, use container_commands to inject your setting into 00_application.conf:
container_commands: 01_add_max_body_size: command: 'echo "client_max_body_size 50M;" >> /etc/nginx/conf.d/elasticbeanstalk/00_application.conf' 02_restart_nginx: command: "service nginx restart"
If EB already has a client_max_body_size line in that file, replace it with sed instead:
container_commands: 01_replace_max_body_size: command: 'sed -i "s/client_max_body_size [0-9]*M;/client_max_body_size 50M;/" /etc/nginx/conf.d/elasticbeanstalk/00_application.conf' 02_restart_nginx: command: "service nginx restart"
Don’t Forget Rails’ Own Upload Limit
Even if Nginx is fixed, Rails has its own default upload cap. Update config/application.rb to match your new Nginx setting:
config.action_dispatch.max_upload_size = 50.megabytes
Verify the Fix
After deploying, SSH into your EB instance and run this command to confirm your setting is active:
nginx -T
Search for client_max_body_size in the output—you should see your 50M value listed as the final active configuration.
内容的提问来源于stack exchange,提问作者antpaw

