加入收藏 | 设为首页 | 会员中心 | 我要投稿 商洛站长网 (https://www.0914zz.com/)- AI应用、CDN、边缘计算、云计算、物联网!
当前位置: 首页 > 运营中心 > Nginx > 正文

如何使用Nginx(healthd)在access_log文件名中使用变量

发布时间:2021-02-21 11:26:06 所属栏目:Nginx 来源:互联网
导读:我在AWS中有一个mutli-container设置.我试图遵循这个:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html但是(使用最新的Nginx - 1.9.12)一旦我尝试在文件名中使用变量,我就会开始在错误日志中看到错误,并且不会

我在AWS中有一个mutli-container设置.我试图遵循这个:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html

但是(使用最新的Nginx – 1.9.12)一旦我尝试在文件名中使用变量,我就会开始在错误日志中看到错误,并且不会创建文件本身.

error.log中:

2016/03/10 05:57:38 [error] 6#6: *1 testing "/etc/nginx/html" existence failed (2: No such file or directory) while logging request,client: xxx.xxx.xxx.xxx,server: localhost,request: "GET /v1/service?staus=ok HTTP/1.1",upstream: "http://xxx.xxx.xxx.xxx:8088/v1/service?staus=ok",host: "xxx.xxx.xxx.xxx"

此配置不起作用:

upstream app_v1 {
  server app_v1:8088;
}

map $http_upgrade $connection_upgrade {
  default        "upgrade";
  ""            "";
}

log_format healthd '$msec"$uri"'
          '$status"$request_time"$upstream_response_time"'
          '$http_x_forwarded_for';

server {
  listen 80;
  server_name localhost;

  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/access.log main;
  access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

  location ~* ^/ {
    proxy_pass         http://app_v1;
    proxy_redirect     off;

    proxy_set_header   Connection      $connection_upgrade;
    proxy_set_header   Upgrade         $http_upgrade;
    proxy_set_header   Host            $host;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

这个工作正常:

upstream app_v1 {
  server app_v1:8088;
}

map $http_upgrade $connection_upgrade {
  default        "upgrade";
  ""            "";
}

log_format healthd '$msec"$uri"'
          '$status"$request_time"$upstream_response_time"'
          '$http_x_forwarded_for';

server {
  listen 80;
  server_name localhost;

  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;

  access_log /var/log/nginx/access.log main;
  access_log /var/log/nginx/healthd/application.log healthd;

  location ~* ^/ {
    proxy_pass         http://app_v1;
    proxy_redirect     off;

    proxy_set_header   Connection      $connection_upgrade;
    proxy_set_header   Upgrade         $http_upgrade;
    proxy_set_header   Host            $host;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

然而,健康服务(增强健康)正在寻找格式化的文件:

/var/log/nginx/healthd/application.log.$year-$month-$day-$hour

我可以使用docker-compose在本地重现这个.我不确定我在这里缺少什么.

马克西姆

最佳答案 这是一个常见的陷阱. Doc于access_log说:

The file path can contain variables (0.7.6+),but such logs have some constraints:

  • during each log write the existence of the request’s root directory is checked,and if it does not exist the log is not created.

您没有设置根目录,因此nginx回退到默认的/ etc / nginx / html并且它不存在.

只需添加root / var / www; (或其他一些现有路径)或者,如zezollo建议的那样,创建/ etc / nginx / html.

(编辑:商洛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读