【问题标题】:AWS EB + Nginx, update access.log format or create new logAWS EB + Nginx,更新 access.log 格式或创建新日志
【发布时间】:2018-11-01 12:05:49
【问题描述】:

我正在 AWS 的 Elastic Beanstalk 上运行一个应用程序,使用运行在 64 位 Amazon Linux/4.5.0 和 Nginx 上的配置 Node.js。

我想将请求标头“X-My-Header”作为字段添加到 access.log。除此之外,我会使用复合默认 nginx 日志 + 我的标头创建一个新的日志文件。我发现了几个关于使用 nginx 进行日志记录的类似问题,但是 EB 方面抛出了一个额外的曲线球,说明如何通过 /.ebextensions 配置文件更新 nginx 配置。

我已经完成创建一个日志文件,但它没有被填充任何东西。我也尝试更新 access.log 文件,但这似乎也没有发生。我看到其他人添加标头将使用格式“$http_”,并且似乎“X-Header-Example”的 http 请求标头被格式化为“$http_header_example”(请参阅​​ nginx 复合默认值中的“$http_user_agent”) ,虽然不想在假设上浪费时间,但请注意我添加了“$http_x-my-header”和“$http_x_my_header”。

尝试 1:更新现有 access.log 格式

files:
  /etc/nginx/conf.d/01_proxy.conf:
      owner: root
      group: root
      content: |
          log_format my_log_format '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" - "$http_x_my_header" - "$http_x-my-header"';
          access_log /var/log/nginx/access.log my_log_format;

结果:access.log 不包含任何附加字段。它甚至没有空的""s 或-

尝试 2:创建新的日志文件

files:
  /etc/nginx/conf.d/01_proxy.conf:
      owner: root
      group: root
      content: |
          log_format my_log_format '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" - "$http_x_my_header" - "$http_x-my-header"';
          access_log /var/log/nginx/new_log.log my_log_format;

结果:当我从 EB 仪表板导出日志时,new_log.log 现在出现在 var/log/nginx 中。但是,它完全是空的。

我读过一些其他类似的问题,提到删除文件和重新启动服务器有时会有所帮助。我尝试重新启动应用程序,甚至通过 EB 仪表板完全重建环境,都没有导致不同的结果。

我的解决方案主要基于this medium article,第 2.1 节。但是,当我尝试将 container_command 添加到我的 .config 文件时,我的整个环境停止工作。我不得不恢复到不同的部署,然后重建环境以使其再次运行。

有什么建议吗?

我的目标是将此请求标头与传入的请求相关联。理想情况下,我可以更新现有的默认 access.log。我将解决一个单独的文件。或者,如果您对我如何能够访问此信息有任何其他建议,我会全力以赴!谢谢。

编辑一个新的尝试:

Here 它表明你可以完全替换默认的 nginx.config,所以我尝试删除我的其他文件,而是将之前的 medium article 中的默认值复制/粘贴到 /.ebextensions/nginx/nginx.config 文件中,除了添加我的那里的变化。我更新了log_format main 以包含我的"$http_x_my_header" 值。

很遗憾,部署失败并显示以下消息:

应用程序版本中的配置文件 .ebextensions/nginx/nginx.config 包含无效的 YAML 或 JSON。 YAML 异常:无效的 Yaml:预期的 '',但在“”,第 7 行,第 1 列中找到标量:包括 /usr/share/nginx/modules ... ^ ,JSON 异常:无效的 JSON:位置上的意外字符 (u) 0..更新配置文件。

违规行是include /usr/share/nginx/modules,它存在并且在中型文章提供的默认值中工作正常。

我希望这是一个肮脏的修复,我至少可以从中获得一些结果,但可惜,它似乎还有另一个障碍。

【问题讨论】:

  • 对于后代:我认为我第一次尝试的一个问题是弹性 beanstalk 有一个 webapp_healthd.conf 文件调用 access_log /var/log/nginx/access.log main; 这将覆盖我设置 access_log /var/log/nginx/access.log my_log_format; 的位置,这就是为什么我那次尝试没有看到任何变化。见这里:medium.com/@marilu597/… 也许一个可行的解决方案是覆盖 main,虽然我不知道这是否可能。

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


【解决方案1】:

我已经通过我自己在类似问题中的回答回答了这个问题:

AWS EB + nginx: Update access.log format to obfuscate sensitive get request parameters

简而言之:对于 Node AWS EB 环境,nginx 配置的 server 指令存在于自动生成的 00_elastic_beanstalk_proxy.conf 文件中。在这里,他们调用access_log /var/log/nginx/access.log main,因此添加一个尝试更改 access_log 的 ebextension 配置会被覆盖。

我的解决方案是双重的:通过上传基于默认值的自定义 nginx.conf 来覆盖主 log_format(AWS 说您可以这样做,但建议您通过拉取默认创建的那个,并在更新时重新检查它环境图像的版本),并且我还必须对自动生成的文件执行相同的操作,以执行一些设置我想要记录的新变量的逻辑。

有关更多详细信息,请参阅上面链接的答案,其中包含有关该过程的更多信息。

【讨论】:

    【解决方案2】:

    我的解决方案是覆盖 nginx.conf。

    nodejs平台的AWS doc是删除现有的/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf,换成自己的配置。我已经测试过它也可以工作。对于 Docker 平台,需要删除 /sites-enabled/elasticbeanstalk-nginx-docker-proxy.conf。

    以下代码仅适用于 docker 平台。版本:Docker 运行在 64 位 Amazon Linux/2.16.7

    您应该在您的平台中找到 nginx.conf 的默认代码。

    将 .ebextensions/nginx/00-my-proxy.config 添加到您的构建文件夹

    files:
      /etc/nginx/nginx.conf:
        mode: "000644"
        owner: root
        group: root
        content: |
            # Elastic Beanstalk Nginx Configuration File
    
            user  nginx;
            worker_processes  auto;
    
            error_log  /var/log/nginx/error.log;
    
            pid /var/run/nginx.pid;
    
            events {
                worker_connections  1024;
            }
    
            http {
              include /etc/nginx/mime.types;
              default_type application/octet-stream;
    
              access_log /var/log/nginx/access.log;
    
              log_format healthd '$msec"$uri"$status"$request_time"$upstream_response_time"$http_x_forwarded_for';
    
              upstream docker {
                  server 172.17.0.2:3000;
                  keepalive 256;
              }
              log_format timed_combined '"$http_x_forwarded_for"'
                          '$remote_addr - $remote_user [$time_local] '
                          '"$request" $status $body_bytes_sent '
                          '"$http_referer" "$http_user_agent" '
                          '$request_time $upstream_response_time $pipe';
    
    
              map $http_upgrade $connection_upgrade {
                      default        "upgrade";
                      ""            "";
              }
    
              server {
                  listen 80;
    
                    gzip on;
                  gzip_comp_level 4;
                  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
                    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 timed_combined;
    
                    location / {
                        proxy_pass            http://docker;
                        proxy_http_version    1.1;
    
                        proxy_set_header    Connection            $connection_upgrade;
                        proxy_set_header    Upgrade                $http_upgrade;
                        proxy_set_header    Host                $host;
                        proxy_set_header    X-Real-IP            $remote_addr;
                        proxy_set_header    X-Forwarded-For        $proxy_add_x_forwarded_for;
                    }
              }
            }
    
    

    在 EB docker 平台中,Nginx 配置中的 server 块在另一个文件中 /etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker-proxy.conf

    server {
        listen 80;
         access_log    /var/log/nginx/access.log;
    }
    

    入口config nginx.conf是

        include       /etc/nginx/conf.d/*.conf;
        include       /etc/nginx/sites-enabled/*;
    

    所以添加这个自定义配置只会导致这个结果 - 空日志文件。

    files:
      /etc/nginx/conf.d/01_proxy.conf:
          owner: root
          group: root
          content: |
              log_format my_log_format '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" - "$http_x_my_header" - "$http_x-my-header"';
              access_log /var/log/nginx/new_log.log my_log_format;
    
       log_format ...
       access_log ...
       server {
           ...
       }
    

    我已经在my github写了详细信息。

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 2020-06-20
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2021-10-12
      • 2017-12-02
      相关资源
      最近更新 更多