【问题标题】:How to configure .ebextensions for nginx location directive?如何为 nginx 位置指令配置 .ebextensions?
【发布时间】:2014-02-15 14:50:40
【问题描述】:

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

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

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

【问题讨论】:

    标签: nginx amazon-elastic-beanstalk


    【解决方案1】:

    嗯嗯! .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
    

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

    哦,请注意您的 YAML 中没有标签!只允许空格...检查日志文件/var/log/cfn_init.log 是否有错误...

    祝你好运!

    【讨论】:

      【解决方案2】:

      这可以使用 .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:1 中不允许使用 2014/05/09 11:46:11 [emerg] 13746#0:“位置" 指令在 /etc/nginx/conf.d/robots.conf:1 中是不允许的```
      • 天哪。是的,好的,显然这无法在全球范围内实现。您需要实际查找并替换 nginx 配置文件本身中的内容。这有点难,但仍然可以实现。
      【解决方案3】:

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

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

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

      按照亚马逊在Using the AWS Elastic Beanstalk Node.js Platform - Configuring the Proxy Server - Example .ebextensions/proxy.config 中给出的示例,我们可以看到他们创建了一个ebextension,该扩展创建了一个名为/etc/nginx/conf.d/proxy.conf 的文件。该文件包含与原始 nginx 配置文件相同的内容。然后,他们使用 container_commands 删除原始 nginx 配置文件。

      您需要将 Amazon 示例替换为当前 nginx 配置文件的内容。注意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"
      

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

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

      第二个选项基于这篇文章:jabbermarky's answer in Amazon forums

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

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

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

      【讨论】:

      • 对于那些使用最新 EB 平台的人来说,stackoverflow.com/a/63812541/3090068 是正确答案,因为 nginx 配置由 Elastic Beanstalk 管理,而在 .ebextensions 中完成的手动配置会被淘汰。
      【解决方案4】:

      似乎上述方法不再起作用。新方法是将 nginx .conf 文件放入 .ebextensions 的子文件夹中:

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

      Source

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

      【讨论】:

      • 我在其他地方读到这仅适用于某些平台,可能不适用于 Docker 平台。仔细检查您使用的平台。
      • 这似乎只记录在 Java 平台上。
      【解决方案5】:

      There is an approach 在 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中的默认文件吗?
      • 如果我想覆盖事件指令,逻辑是什么? events { worker_connections 6144; }
      • 这是迄今为止最好的解决方案。它正确使用了以下行:include conf.d/elasticbeanstalk/*.conf; in /etc/nginx/nginx.conf。对于未来的旅行者,您可以通过运行eb ssh然后运行ls -al /etc/nginx/conf.d/elasticbeanstalk/来确认文件已成功复制到预期的目录。
      【解决方案6】:

      这对我有用:

      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
      

      【讨论】:

        【解决方案7】:

        对于 Amazon Linux 2 版本,在您的捆绑包中使用此路径并将这些文件夹压缩在一起

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

        【讨论】:

        • 我可以确认这对我有用!
        猜你喜欢
        • 2012-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 2020-09-17
        • 2012-04-22
        • 2012-11-07
        • 2020-10-17
        相关资源
        最近更新 更多