如何在AWS Elastic Beanstalk(Linux 1)部署时修改Nginx配置
Elastic Beanstalk Linux 1平台修改Nginx超时与client_max_body_size配置
问题背景
在AWS Elastic Beanstalk Linux 1平台部署Django应用,尝试通过.ebextensions/nginx.config自定义Nginx配置(设置超时参数和client_max_body_size),但部署后Nginx仍保持默认配置。
问题原因
Linux 1平台的Nginx默认server块配置位于/etc/nginx/conf.d/elasticbeanstalk/00_application.conf,原配置在/etc/nginx/conf.d/下创建的全局配置会被server块内的默认参数覆盖,导致自定义设置不生效;同时原配置的Nginx重启指令可能未正确触发。
解决方案一:添加server级自定义配置
替换.ebextensions/nginx.config为以下内容,将自定义参数直接注入到server块的作用域内:
files: # 在Elastic Beanstalk的Nginx配置目录下创建自定义文件,会被默认server配置加载 "/etc/nginx/conf.d/elasticbeanstalk/custom.conf": mode: "000644" owner: root group: root content: | client_header_timeout 900; client_body_timeout 900; send_timeout 900; proxy_connect_timeout 900; proxy_read_timeout 900; proxy_send_timeout 900; client_max_body_size 500M; container_commands: # 强制重启Nginx确保配置生效 01_restart_nginx: command: "sudo service nginx restart" ignoreErrors: true
解决方案二:直接修改默认server配置文件
如果方案一未生效,可通过sed命令直接在默认server块中插入参数,确保优先级最高:
container_commands: 01_insert_custom_nginx_params: command: | sudo sed -i '/server {/a \ client_header_timeout 900;\ client_body_timeout 900;\ send_timeout 900;\ proxy_connect_timeout 900;\ proxy_read_timeout 900;\ proxy_send_timeout 900;\ client_max_body_size 500M;' /etc/nginx/conf.d/elasticbeanstalk/00_application.conf 02_restart_nginx: command: "sudo service nginx restart" ignoreErrors: true
额外优化建议
- 移除buildspec.yml冗余操作:
.ebextensions/django.config已配置leader_only的迁移命令,建议删除buildspec中pre_build阶段的migrate步骤,避免重复执行导致冲突。 - 验证配置有效性:部署完成后登录EC2实例,执行以下命令确认配置:
- 查看自定义配置文件:
cat /etc/nginx/conf.d/elasticbeanstalk/custom.conf - 验证Nginx配置语法:
nginx -t - 确认Nginx服务状态:
service nginx status
- 查看自定义配置文件:
内容的提问来源于stack exchange,提问作者Gregory
相关产品推荐
相关产品推荐

