【问题标题】:Nginx config file overwritten during Elastic Beanstalk deployment?在 Elastic Beanstalk 部署期间覆盖 Nginx 配置文件?
【发布时间】:2014-09-11 16:23:00
【问题描述】:

我需要将 p3p 标头添加到标准 Nodejs 和 Nginx Elastic Beanstalk 上的静态资源位置。

我创建了一个ebextension 脚本,如this question 中所述。该脚本使用 set 在静态位置指令下的alias 行下添加add_header 指令。它在/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf 文件上运行。

脚本不仅修改文件,还将其复制到“安全”位置,即 /home/ec2-user。根据/var/log/cfn-init.log,脚本运行正常。作为证据,修改后的文件的副本在正确的位置显示了附加的标题。但是/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf文件没有这个修改

我只能推断,虽然我的脚本运行良好,但部署过程中的其他东西会覆盖它。这很奇怪,因为according to documentation容器命令是在应用程序和Web服务器设置后运行的,所以我看不到它是做什么的。

那么 ho/什么正在覆盖这个文件,我该如何防止呢?

【问题讨论】:

    标签: amazon-web-services nginx amazon-elastic-beanstalk


    【解决方案1】:

    要修改配置文件而不被覆盖,解决方法是修改位于/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf的模板文件

    我更新这个文件来添加所需的指令,它会自动部署到/etc/nginx/conf.d,修改是活动的。

    【讨论】:

    • 除了通过位于.ebextensions 文件夹中的配置文件中的files: 块修改此文件之外,还有其他方法吗?尽管一切似乎都是正确的,但该文件从未被修改过。我只在/tmp/deployment/config 文件夹和/etc/nginx/ 文件夹中看到了默认文件。
    • 是的,您可以使用配置文件的 commandscontainer_commands 部分运行代码。见docs.aws.amazon.com/elasticbeanstalk/latest/dg/…
    • files: 不起作用。04modify_nginx_conf: command: sed -i '/EB_INCLUDE/a geoip_country /usr/share/GeoIP/GeoIP.dat;\ngeoip_city /usr/share/GeoIP/GeoLiteCity.dat;' /tmp/deployment/config/#etc#nginx#nginx.conf 不起作用。 /etc/nginx/nginx.conf 永不改变!我真的很生气。
    • 如果你尝试将你的配置放在一个单独的文件中,比如00_elastic_beanstalk_proxy.conf
    • 这不再有效。 @philip-callender 的评论是正确的。
    【解决方案2】:

    Elastic Beanstalk 似乎发生了变化,通常推荐的覆盖#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf 的方法/hack 不再有效。也不会在 /tmp/deployment/config 中创建 任何 文件。

    我找到的解决方案是使用 container_commands 指令直接覆盖 /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf,因为这些命令是在 Elastic Beanstalk 安装创建它的 nginx 版本之后执行的配置。

    来自http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-container-commands

    它们 [container_commands] 在设置应用程序和 Web 服务器并提取应用程序版本文件之后,但在部署应用程序版本之前运行。

    我在 .ebextensions 中分三个步骤完成此操作:

    1. 创建我的 nginx 配置文件版本。

    2. 创建一个脚本来用我自己的覆盖标准配置文件。

    3. 运行脚本。

    前两个步骤发生在安装过程的较早阶段,而最后一个步骤使用 container_commands,因此如前所述,发生在安装过程的后期。

    这是我使用的文件:

    文件 .ebextensions/install_nginx_config_01.config
    (注意缩进很重要)

    #
    #   STEP 1 - Create the nginx config file
    #
    files:
    
      "/tmp/my.nginx.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
          # This file was overwritten during deployment
          # by .ebextensions/install_nginx_config_03.config
    
          upstream nodejs {
              server 127.0.0.1:3000;
              keepalive 256;
          }
    
          server {
              listen 8080;
    
              location / {
                  proxy_pass  http://nodejs;
                  proxy_set_header   Connection "";
                  proxy_http_version 1.1;
                  proxy_set_header        Host            $host;
                  proxy_set_header        X-Real-IP       $remote_addr;
                  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
              }
    
              gzip on;
              gzip_comp_level 4;
              gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
          }
    

    文件 .ebextensions/install_nginx_config_02.config

    #
    #   STEP 2 - Create a script that will overwrite the Nginx config
    #
    files:
    
      "/tmp/install-nginx-config.sh" :
        mode: "000755"
        owner: root
        group: root
        content: |
          #!/bin/sh
          cp /tmp/my.nginx.conf /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
    

    文件 .ebextensions/install_nginx_config_03.config

    #
    #   STEP 3 - Run the script to overwrite the nginx config template.
    #
    container_commands:
    
      01_runmyshellscript:
        command: "/tmp/install-nginx-config.sh"
    

    【讨论】:

    • 这似乎是目前唯一有效的方法
    • 当我尝试这个时,我得到:服务:AmazonCloudFormation,消息:[/Resources/AWSEBAutoScalingGroup/Metadata/AWS::CloudFormation::Init/prebuild_1_Homepage/files//tmp/install-nginx-config。 sh] 模板中不允许使用“null”值
    • 谢谢!我终于使用这种方法完成了这项工作。可以注意到,如果有人愿意,所有三个步骤都可以放在一个文件中(在这种情况下,要写入的两个文件都必须在一个文件子句下)。
    【解决方案3】:

    在花费了几乎一整天时间并尝试了所有可能的解决方案之后,截至 2017 年 7 月 17 日,上述解决方案不起作用。 对我来说,我想替换 /etc/nginx/conf.d/elasticbeanstalk/00_application.conf 我在我的 .ebextension 文件夹中创建了下面显示的文件夹结构,该文件被我的内容覆盖。该解决方案也适用于位于 /etc/nginx 文件夹中的 nginx.conf

    【讨论】:

    • 谢谢,也为我工作。此行为记录在 here您现在可以在 .ebextensions/nginx 文件夹中放置一个 nginx.conf 文件来覆盖 Nginx 配置。您还可以将配置文件放在 .ebextensions/nginx/conf.d 文件夹中,以便将它们包含在平台提供的 Nginx 配置中。
    • 提及该行为的博文的直接链接是aws.amazon.com/blogs/aws/…
    • 值得一提的是,此解决方案仅适用于某些 ELB 平台 - Java 和 Go。例如,如果您使用带有 nginx 作为代理的 Docker 平台,您必须使用配置文件(使用 files 关键字)放置在 .ebextensions 中的解决方案 - 下面的示例 files: "/etc/nginx/nginx.conf": content: | #your override nginx config here
    • 我可以确认在撰写本文时这不适用于他们最新的节点平台。
    • 为 Ruby Amazon Linux 2 放入 ./platform/nginx/* 有效。在文档docs.aws.amazon.com/elasticbeanstalk/latest/dg/… 中说明。
    【解决方案4】:

    在撰写本文时,将值更新/添加到 nginx.conf 文件中的 http 配置而不覆盖它的正确方法是添加 .config文件到如下所示的.ebextensions 文件夹:

    files:
      "/etc/nginx/conf.d/custom_nginx.conf":
        content: |
    
          proxy_connect_timeout       600;
          proxy_send_timeout          600;
          proxy_read_timeout          600;
          send_timeout                600;
    

    这会在/etc/nginx/conf.d 目录中创建一个名为custom_nginx.conf 的新文件。由于nginx.conf 文件包含

    http {
      include       /etc/nginx/conf.d/*.conf;
    }
    

    当服务器启动时,它会将 4 个超时变量从 custom_nginx.conf 拉到 nginx.conf 的 http 部分

    【讨论】:

    • 你能帮我如何覆盖 nginx.conf 文件。我需要添加 http{underscores_in_headers on;}
    • 在撰写本文时,nginx.conf 文件实际上位于此处:/etc/nginx/nginx.conf
    【解决方案5】:

    以下是亚马逊截至 2018 年 8 月的最新说明:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/nodejs-platform-proxy.html

    (我刚刚使用这些说明为 Elastic Beanstalk 上的 Node.js 应用程序自定义 Nginx 代理,它按预期工作。)

    基本上你为 Nginx 使用你自己的 proxy.conf,并删除自动生成的东西。

    # .ebextensions/proxy.config
    files:
      /etc/nginx/conf.d/proxy.conf:
        mode: "000644"
        owner: root
        group: root
        content: |
          upstream nodejs {
            server 127.0.0.1:5000;
            keepalive 256;
          }
    
          server {
            listen 8080;
    
            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/healthd/application.log.$year-$month-$day-$hour healthd;
            access_log  /var/log/nginx/access.log  main;
    
            location / {
                proxy_pass  http://nodejs;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    
            gzip on;
            gzip_comp_level 4;
            gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
            location /static {
                alias /var/app/current/static;
            }
    
          }
    
      /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
        mode: "000755"
        owner: root
        group: root
        content: |
          #!/bin/bash -xe
          rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
          service nginx stop 
          service nginx start
    
    container_commands:
     removeconfig:
        command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"
    

    【讨论】:

      【解决方案6】:

      截至 2020 年 8 月,Ruby 2.6 running on 64bit Amazon Linux 2/3.1.0

      nginx 文件放在.platform/nginx/ 中对我有用。

      这是我的文件夹结构:

      【讨论】:

      • 花了一天的时间后,这是唯一对我有用的东西。 ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux] Amazon Linux 2
      • AWS 文档在此处进行了介绍:docs.aws.amazon.com/elasticbeanstalk/latest/dg/… 有关详细信息,请参阅标题为“反向代理配置”的部分。
      猜你喜欢
      • 2019-02-15
      • 2015-08-26
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2020-09-29
      • 2014-11-22
      • 2019-09-24
      相关资源
      最近更新 更多