首页 > 解决方案 > AWS Beanstalk 无法在部署时自动创建 nginx 配置文件

问题描述

我创建了一个部署在 AWS Beanstalk Amazon Linux 2 (Python 3.8) 上的 Django API 应用程序。在我的应用程序的一部分中,用户应该能够通过 API 上传文件。

默认情况下,当上传的文件大于 3 MB 并在 Nginx 日志中记录“用户尝试上传大文件”时,Nginx 会向用户返回 CORS 错误。

唯一对我有用的解决方案是创建波纹管配置文件并重新加载 Nginx:

/etc/nginx/conf.d/proxy.conf

client_max_body_size 50M;

进而: sudo service nginx reload

我通过 SSH 连接到我的 beanstalk 应用程序的主机 EC2 手动完成了这个过程。我想在每个部署和每个实例中自动执行此过程。

我在位于我的项目根目录的文件夹中 创建了一个名为nginx_max_upload.configfile 的文件::.ebextensionsnginx_max_upload.config

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

commands:
  reload_nginx:
    command: "sudo service nginx reload"
    ignoreErrors: true

我的问题是,上面的代码没有在指定目录中创建我想要的文件。当我将目录更改为另一个目录/usr/local/bin/proxy.conf时,文件将成功创建,但无法在 Nginx 配置文件夹中创建配置文件。

我想问题可能出在权限上,但我不知道如何向部署代理授予所需的权限。

另外,我已经尝试了这两种解决方案,但它们都不起作用:

  1. 我试图在另一个文件夹中创建配置文件,然后通过mv命令将其移动到正确的目录,但它没有用。
  2. Also, I have tried to put the creation code in the predeply hook and put manual echo commands in my code. I saw all of my echo commands output in the beanstalk logs but it didn't do anything (nor creating the file in the nginx configuration neither moving it from somewhere else to the configuration folder).

标签: amazon-web-servicesnginxamazon-elastic-beanstalk

解决方案


Since you are using Amazon Linux 2 (AL2), your configuration files are incorrect. They used to work in AL1, but for AL2, they are in different place and have different format as shown in the docs.

Thus could have the following .platform/nginx/conf.d/myconfig.conf (not in .ebextensions) with content:

client_max_body_size 50M;

推荐阅读