如何为nginx的location指令配置.ebextensions?

14

我想在Elastic Beanstalk中配置我的预发布环境,以始终禁止所有蜘蛛爬行。nginx指令应如下所示:

location /robots.txt {
    return 200 "User-agent: *\nDisallow: /";
}

我知道我想在 .ebextensions/ 文件夹下创建一个文件,例如 01_nginx.config,但我不确定如何构建其中的 YAML 以使其工作。我的目标是将此位置指令添加到现有配置中,而不必完全替换任何已存在的配置文件。

8个回答

14

有一种方法是在Amazon Linux 2上使用更新的.platform/nginx配置扩展(而不是旧的AMI)。

默认的nginx.conf在整个nginx.conf文件的两个位置包含配置部分。一个在http块内部,因此您无法在此处放置其他的location块,因为这在语法上是不合法的。第二个位于server块内部,这就是我们需要的。

这个第二个位置的局部文件是从一个特殊的子目录.platform/nginx/conf.d/elasticbeanstalk中包含的。将您的位置片段放置在这里以添加位置块,如下所示:

# .platform/nginx/conf.d/elasticbeanstalk/packs.conf
location /packs {
    alias /var/app/current/public/packs;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
}

你是否仍需要执行container_command来替换/删除/etc/nginx/conf.d/elasticbeanstalk/00_application.conf中的默认文件? - A13X
如果我想要覆盖“events”指令,那么逻辑应该是什么? events { worker_connections 6144; } - inside
1
这绝对是最好的解决方案。它在/etc/nginx/nginx.conf中恰当地使用了一行代码:include conf.d/elasticbeanstalk/*.conf;。对于未来的旅行者,您可以通过运行eb ssh并执行 ls -al /etc/nginx/conf.d/elasticbeanstalk/ 来确认文件已成功复制到预期目录中。 - stwr667

11

我想做同样的事情。经过大量挖掘,我找到了两种方法:

选项1.使用ebextension将nginx配置文件替换为您的自定义配置

我选择了这个选项,因为它是最简单的。

按照Amazon在使用AWS Elastic Beanstalk Node.js平台-配置代理服务器-示例.ebextensions/proxy.config中给出的示例,我们可以看到他们创建了一个名为/etc/nginx/conf.d/proxy.conf的文件的ebextension。该文件包含与原始nginx配置文件相同的内容。然后,他们使用container_commands删除原始的nginx配置文件。

您需要用当前nginx配置文件的内容替换Amazon示例。请注意,在容器命令中要删除的nginx配置文件也必须更新。我使用的文件是:

  • nginx配置文件1: /opt/elasticbeanstalk/support/conf/webapp_healthd.conf
  • nginx配置文件2: /etc/nginx/conf.d/webapp_healthd.conf

因此,对我有效的最终ebextension如下:

/.ebextensions/nginx_custom.config

# Remove the default nginx configuration generated by elastic beanstalk and
# add a custom configuration to include the custom location in the server block.
# Note that the entire nginx configuration was taken from the generated /etc/nginx/conf.d/webapp_healthd.conf file
# and then, we just added the extra location we needed.

files:
  /etc/nginx/conf.d/proxy_custom.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream my_app {
        server unix:///var/run/puma/my_app.sock;
      }

      log_format healthd '$msec"$uri"'
                      '$status"$request_time"$upstream_response_time"'
                      '$http_x_forwarded_for';

      server {
        listen 80;
        server_name _ localhost; # need to listen to localhost for worker tier

        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
          set $year $1;
          set $month $2;
          set $day $3;
          set $hour $4;
        }

        access_log  /var/log/nginx/access.log  main;
        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

        location / {
          proxy_pass http://my_app; # match the name of upstream directive which is defined above
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /assets {
          alias /var/app/current/public/assets;
          gzip_static on;
          gzip on;
          expires max;
          add_header Cache-Control public;
        }

        location /public {
          alias /var/app/current/public;
          gzip_static on;
          gzip on;
          expires max;
          add_header Cache-Control public;
        }

        location /robots.txt {
          return 200 "User-agent: *\nDisallow: /";
        }
      }

container_commands:
  # Remove the default nginx configuration generated by elastic beanstalk
 removeconfig:
    command: "rm -f /opt/elasticbeanstalk/support/conf/webapp_healthd.conf /etc/nginx/conf.d/webapp_healthd.conf"

一旦您部署此更改,您必须重新加载nginx服务器。您可以使用eb ssh your-environment-name连接到服务器,然后运行sudo service nginx reload

选项2. 使用ebextension修改nginx配置文件生成器,以便在最终的nginx配置文件中包含您的自定义位置

第二个选项基于这篇文章:jabbermarky's answer in Amazon forums 他在他的答案中很好地解释了这种方法,因此如果您想实施它,我建议您阅读它。如果您要实施此答案,则需要更新nginx文件配置生成器的位置。
请注意,我尚未测试此选项。
总之,他添加了一个要在生成nginx配置文件之前执行的shell脚本。在此shell脚本中,他修改了nginx配置文件生成器,以在生成的nginx配置文件中包含他想要的服务器块位置。最后,他添加了一个文件,其中包含最终nginx配置文件的服务器块中所需的位置。

2
对于使用最新的EB平台的用户,https://dev59.com/pmEi5IYBdhLWcg3wYLaT#63812541 是正确的答案,因为nginx配置由Elastic Beanstalk管理,而在.ebextensions中进行的手动配置会被清除。 - Yuki Inoue

5
似乎之前提到的方法不再起作用了。新的方法是将nginx.conf文件放入.ebextensions子文件夹中:

您现在可以将nginx.conf文件放置在.ebextensions/nginx文件夹中,以覆盖Nginx配置。您还可以将配置文件放置在.ebextensions/nginx/conf.d文件夹中,以便将它们包含在平台提供的Nginx配置中。

来源

这样做也不需要重新启动nginx,因为Elastic Beanstalk会处理这个问题。

我在其他地方读到过这只适用于某些平台,可能不适用于Docker平台。请仔细检查您正在使用的平台。 - justin.m.chase
1
这似乎只有Java平台有相关文档记录。 - Jesse Buchanan

3

嗯!.ebextensions

最简单的方法可能是创建一个shell脚本来更改您的配置,然后运行它。虽然我不太了解nginx,但您可以尝试以下内容:

files:
    "/root/setup_nginx.sh" :
    mode: "000750"
    owner: root
    group: root
    content: |
        #!/bin/sh
        #  Configure for NGINX
        grep robots.txt <your_config_file> > /dev/null 2>&1
        if [ $? -eq 1 ] ; then
            echo < EOF >> <your_config_file>
            location /robots.txt {
                return 200 "User-agent: *\nDisallow: /";
            }
            EOF
        # Restart any services you need restarting
        fi

container_commands:
    000-setup-nginx:
        command: /root/setup_nginx.sh

即,首先创建一个能够完成你所需的shell脚本,然后运行它。

哦,还要小心,你的YAML中不能有制表符!只允许使用空格… 检查日志文件/var/log/cfn_init.log是否有错误...

祝好运!


3

对于 Amazon Linux 2 版本,请在捆绑包中使用以下路径,并将这些文件夹压缩到一起:

.platform/nginx/conf.d/elasticbeanstalk/000_my_custom.conf


我可以确认这对我起作用了! - Chris Parry

0

要解决这个问题 - 你需要包装你的配置文件。如果你使用Docker,你应该有一个zip文件(我的称为deploy.zip),其中包含你的Dockerrun.aws.json。如果没有 - 修改起来相当容易,只需通过以下命令将部署压缩成zip格式:zip -r deploy.zip Dockerrun.aws.json

有了这个 - 现在你需要添加一个.platform文件夹,具体如下:

├── .platform
│   └── nginx
│       └── conf.d
│           └── custom.conf

你可以随意命名你的custom.conf文件,并且可以有任意数量的文件。在custom.conf文件中,你只需要将以下内容放入其中即可。

client_max_body_size 50M;

或者您可以按照自己的配置进行修改。通过这样做,将您的 zip 文件进行修改:

zip -r deploy.zip Dockerrun.aws.json

然后进行部署。您的 Nginx 服务器现在将会遵循新的命令。


0

这是对我有效的方法:

files:
   "/etc/nginx/conf.d/01_syncserver.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      # 10/7/17; See https://github.com/crspybits/SyncServerII/issues/35
      client_max_body_size 100M;
      # SyncServer uses some http request headers with underscores
      underscores_in_headers on;
      # 5/20/21; Trying to get the load balancer to respond with a 503
      server {
        listen 80;
        server_name _ localhost; # need to listen to localhost for worker tier
        location / {
          return 503;
        }
      }

container_commands:
  01_reload_nginx:
    command: pgrep nginx && service nginx reload || true

-1

这可以通过使用.ebextension配置文件来实现,但是在更改其配置文件后,我很难让nginx重新启动。

# .ebextensions/nginx.config
files:
  "/etc/nginx/conf.d/robots.conf":
    mode: "000544"
    owner: root
    group: root
    content: |
      location /robots.txt {
        return 200 "User-agent: *\nDisallow: /";
      }
    encoding: plain

现在,我已经做了类似的事情来添加一个文件来测试nginx,但是出于某种奇怪的原因它没有执行:
"/opt/elasticbeanstalk/hooks/appdeploy/enact/03_restart_nginx.sh":
  mode: "000755"
  owner: root
  group: root
  content: |
    #!/usr/bin/env bash
    . /opt/elasticbeanstalk/containerfiles/envvars
    sudo service nginx restart
    ps aux | grep nginx > /home/ec2-user/nginx.times.log
    true
  encoding: plain

实际上我尝试过这个,但完全失败了。从日志中看到:------------------------------------- /var/log/nginx/error.log ------------------------------------- 2014/05/09 11:46:09 [emerg] 13740#0: 在 /etc/nginx/conf.d/robots.conf 中不允许使用 "location" 指令:1 2014/05/09 11:46:11 [emerg] 13746#0: 在 /etc/nginx/conf.d/robots.conf 中不允许使用 "location" 指令:1 - rgareth
哦,糟糕了。是的,显然这不能在全局范围内实现。您需要在nginx配置文件本身中查找和替换内容。这有点困难,但仍然可以实现。 - jufemaiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接