如何为nginx位置指令配置.ebextensions?

rga*_*eth 6 nginx amazon-elastic-beanstalk

我想在Elastic Beanstalk中配置我的暂存环境,以便始终禁止所有蜘蛛.nginx指令看起来像这样:

location /robots.txt {
    return 200 "User-agent: *\nDisallow: /";
}
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ris 10

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

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

我使用这个选项是因为它是最简单的选项.

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

您需要使用当前nginx配置文件的内容替换Amazon示例.请注意,必须更新要在containter命令中删除的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"
Run Code Online (Sandbox Code Playgroud)

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

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

第二个选项基于这篇文章:jabbermarky在亚马逊论坛中的回答

他在答案中很好地解释了这个方法,所以如果你想实现它,我鼓励你阅读它.如果要实现此答案,则需要更新nginx文件配置生成器的位置.

请注意,我还没有测试过这个选项.

总之,他添加了一个在生成nginx配置文件之前执行的shell脚本.在这个shell脚本中,他修改了nginx配置文件生成器,以在生成的nginx配置文件中包含他想要的服务器块位置.最后,他在最终的nginx配置文件的服务器块中添加了一个包含他想要的位置的文件.

  • 对于那些使用最新 EB 平台的人来说,/sf/answers/4466877901/ 是正确答案,因为 nginx 配置由 Elastic Beanstalk 管理,并且在“.ebextensions”中完成的手动配置被清除。 (2认同)

feb*_*ing 8

有一种方法.platform/nginx在 Amazon Linux 2 上使用更新的配置扩展(而不是旧的 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;
}
Run Code Online (Sandbox Code Playgroud)


Ang*_*erd 5

看来上述方法不再有效了。新方法是将 nginx .conf 文件放入 .ebextensions 中的子文件夹中:

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

来源

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


jok*_*ker 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
Run Code Online (Sandbox Code Playgroud)

即,首先创建一个执行您需要的操作的 schel 脚本,然后运行它。

哦,请小心 YAML 中没有选项卡!只允许使用空格...检查日志文件中/var/log/cfn_init.log是否有错误...

祝你好运!