AWS EB + Nginx,更新access.log格式或创建新日志。

5
我正在AWS的弹性Beanstalk上运行一个应用程序,使用配置Node.js运行在64位Amazon Linux/4.5.0上,带有Nginx。
我想将请求头“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没有包含任何额外的字段。它甚至没有空的"",或者-
尝试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。我会接受一个单独的文件。或者,如果您有任何其他建议,以便我能够获得此信息的访问权限,我非常乐意听取!谢谢。

编辑:新尝试:

这里显示您可以完全替换默认的nginx.config,因此我尝试删除我的其他文件,然后将之前中等文章中的默认文件复制/粘贴到 / .ebextensions / nginx / nginx.config 文件中,除了添加我的更改。 我更新了log_format main以包括我的"$ http_x_my_header"值。

不幸的是,部署失败并出现以下消息:

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

有问题的行是include /usr/share/nginx/modules,在中等文章提供的默认设置中存在且正常工作。

我希望这只是一个临时解决方案,至少能得到一些结果,但不幸的是,它似乎又遇到了另一个障碍。

为了记录: 我认为我的第一次尝试中遇到的问题之一是 Elastic Beanstalk 有一个webapp_healthd.conf文件,其中调用了access_log /var/log/nginx/access.log main;这将覆盖我设置的地方 access_log / var / log / nginx / access.log my_log_format;,这就是为什么我在那次尝试中看不到更改的原因。请参见此处: https://medium.com/@marilu597/getting-to-know-and-love-aws-elastic-beanstalk-configuration-files-ebextensions-9a4502a26e3c 或许一个可行的解决方案是覆盖main,但我不知道是否可能。 - Jake T.
2个回答

3
我已经通过类似问题的回答来回答这个问题:

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

简而言之:对于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表示您可以这样做,但建议您拉取默认创建的版本,并在更新环境镜像的版本时重新检查它),并且我还必须对自动生成的文件执行相同的操作,以执行一些逻辑来设置我想要记录的新变量。有关详细信息,请参见上面链接的答案,其中有关于该过程的更多信息。

1
我的解决方案是覆盖nginx.conf文件。 AWS文档中针对nodejs平台的解决方案是删除现有的/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配置中的服务器块位于另一个文件中:/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中写了详细信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接