5

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

我想将请求标头“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;

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

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

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

有小费吗?

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

编辑一个新的尝试:

在这里它表明你可以完全替换默认的 nginx.config,所以我尝试删除我的其他文件,而是从之前的媒体文章中复制/粘贴默认值到一个/.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,它在中篇文章提供的默认值中存在并且工作正常。

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

4

2 回答 2

3

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

AWS EB + nginx:更新 access.log 格式以混淆敏感的获取请求参数

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

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

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

于 2018-08-20T16:27:23.830 回答
1

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

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

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

您应该在您的平台中找到 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;
}

条目配置 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 {
       ...
   }

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

于 2021-05-05T22:33:19.753 回答