在 AWS Elastic Beanstalk 中自定义 Nginx 配置

Customizing Nginx Configuration in AWS Elastic Beanstalk

我是 运行 rails Ruby 2.0/Puma 实例上的应用程序,我正在尝试自定义 nginx 配置。我需要增加允许的请求大小以允许文件上传。我发现其他一些帖子让我将其添加到我的 .ebextensions 中:

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

这确实按预期创建了文件,但在我手动重新启动 nginx 之前它似乎不起作用。因此,我试图找出一种使用 .ebextensions 命令重新启动 nginx 的方法,但没有取得任何成功。有谁知道用 .ebextensions 重启 nginx 的方法或者知道解决这个问题的更好方法吗?

这是我的配置,对我有用。您必须将它包含在 http 块中。

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

我找到了一种在部署后使用 运行 post 部署脚本的未记录技术重新启动 nginx 的方法。我将其添加到我的 .ebextensions:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      service nginx restart

要重新加载 nginx 配置,您可以使用 container_commands

来自http://www.infoq.com/news/2012/11/elastic-beanstalk-config-files

The container_commands key allows you to execute commands for your container. They are run after the application and web server have been set up and the application has been extracted, but before the application is deployed. container_commands are processed in lexicographical order by name.

container_commands:
  01_reload_nginx:
    command: "service nginx reload"

我是这样工作的。无需重新启动或重新加载 nginx,因为 commands(而不是 container_commands)运行在应用程序部署之前。

commands: 
  01-get-nginx-conf-file:
    command: "aws s3 cp s3://somepath/nginx.conf /home/ec2-user"
  02-replace-default-nginx-config:
    command: "cp /home/ec2-user/nginx.conf /etc/nginx/nginx.conf"

我在这里的回复可能有点晚,但我发现了另一种在 Elastic Beanstalk 上配置 nginx 的侵入性较小的方法。 您可以通过创建.ebextensions/nginx/conf.d 目录直接为nginx 指定配置文件。在 EB 部署期间,在其中找到的任何配置文件都会自动复制到您的 /etc/nginx/conf.d/ 目录。 这似乎是一个更可靠的解决方案。

此处提供文档: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-nginx.html

编辑:正如评论中所指出的,Elastic Beanstalk 在平台之间的实现不一致。此处的文档适用于 Java SE,并且该文档似乎并不适用于所有平台。

我在 Docker 部署到 Elastic Beanstalk 时遇到了类似的情况。我能够在此处使用单个配置文件解决更改以及 nginx 重新加载问题:<app>/.ebextensions/increase_upload_size.config 包括以下代码:

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

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

更改是在我在 EB 中执行 "Upload and Deploy" 时实施的。

以下对我有用(我将 HTTP 负载增加到 100M - 如果您想增加到其他大小,请进行调整):

files:
  "/etc/nginx/conf.d/proxy.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      client_max_body_size 100M;
  "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      service nginx restart