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

前端之家收集整理的这篇文章主要介绍了如何使用Nginx(healthd)在access_log文件名中使用变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在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.

猜你在找的Nginx相关文章