Variables in log name - nginx

I have the following default server setup in nginx:

# Default HTTP Server
server {
  listen 80 default;
  server_name _;
  access_log /var/log/nginx/$server_name.access.log;
  error_log /var/log/nginx/$server_name.error.log;

  server_name_in_redirect off;

  location / {
    root   domain.com/public;
    index  index.php;
    try_files $uri index.php;
  }

  location ~     \.(html|jpg|jpeg|gif|png|ico|css2|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
    root /path/to/domain.com/public;
    expires 30d;
    break;
  }

  charset utf-8;

  location ~ \.php$ {
    include /opt/nginx/conf/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  /path/to/domain.com/public/index.php;   
  }

  location ~ \.(js|ico|gif|jpg|png|css)$ {
    root /path/to/domain.com/public;
  }
}

I have several domains pointing to the server. What I'm trying to accomplish here is to have logs in the format of either /var/log/nginx/mydomain.com/access.log or /var/log/nginx/mydomain.com.access.log

Instead, I am getting /var/log/nginx/$server_name.access.log.

If I try the directory method I get an error when checking the config nginx: [emerg] open() "/var/log/nginx/$server_name/access.log" failed (2: No such file or directory)

Why isn't nginx passing the variable to the file name?

Using nginx/1.0.0


Solution 1:

You need to use the $host variable – only allowed for access_log directives.