大网数据广告活动
服务器运维问答

How to use variables in access_log filename with Nginx (healthd)

· 52CE 我爱测

I have a mutli-container setup in AWS. I'm trying to follow this: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html However (using latest Nginx - 1.9.12) as soon as I try to use variables in th…

问题原文

原帖链接 · maxwell2022 · CC BY-SA 3.0

I have a mutli-container setup in AWS. I'm trying to follow this: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html

However (using latest Nginx - 1.9.12) as soon as I try to use variables in the filename I start to see errors in the error log, and the file itself is not created.

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"

This configuration is not working:

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;
  }
}

And this one is working fine:

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;
  }
}

However the healthd service (enhance health) is looking for a file formatted:

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

I can reproduce this locally using docker-compose. I'm not sure what I'm missing here.

Maxime

已采纳解答

原帖链接 · Alexey Ten · CC BY-SA 3.0

That's a common pitfall. Doc on access_log says:

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.

You didn't set root directory so nginx falls back to default one that is /etc/nginx/html and it doesn't exists.

Just add root /var/www; (or some other existing path) or, as zezollo suggested, create /etc/nginx/html.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。