# The access log in nginx



## zhongyb (Sep 2, 2012)

I configured several virtual hosts in nginx 1.0.14 using regular expression, and configured dedicated access log for every hosts in /var/log. but the following error message is always displayed in nginx-error.log.



> 2012/09/02 21:03:28 [crit] 846#0: *70 open() "/usr/local/etc/nginx//var/log/cdxmlxx.com-access.log" failed (2: No such file or directory) while logging reques



The following is the core code in nginx.conf


```
server {
                listen          80;
                index           index.php index.html;
                server_name     ~^(www\.)?(.+)$;
                set             $www_root /usr/local/www/$2;
                root            $www_root;
                set             $access_log /var/log/$2-access.log;
                access_log      $access_log;

                location ~ \.php$ {
                        fastcgi_pass    127.0.0.1:9000;
                        fastcgi_index   index.php;
                        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param  PATH_INFO        $fastcgi_script_name;
                        include        fastcgi_params;
                }
        }
```

Anybody can help me?


----------



## Morte (Sep 10, 2012)

I'm not sure why $2 is getting populated with a path, which is part of the problem. Nginx documentation has this example you can try for the capture.


```
server {
  server_name   ~^(www\.)?(?<domain>.+)$;
  root  /sites/$domain;
}
```

It looks like you're trying to write files to /var/log. As nginx may randomly write files based off of external factors, it would be a good idea to make a directory such as /var/log/nginx and put files in there instead. An alternative would be to use a relative path, then symlink /usr/local/nginx/log to /var/log/nginx or something like that.


----------



## quintessence (Sep 11, 2012)

Hello,

replace


```
set             $access_log /var/log/$2-access.log;
                access_log      $access_log;
```

with


```
access_log /desired_log_dir/$2-access.log;
```

where desired_log_dir is a directory where user which is running nginx (probably www) should be able to write.


----------



## zhongyb (Oct 3, 2012)

Morte said:
			
		

> I'm not sure why $2 is getting populated with a path, which is part of the problem. Nginx documentation has this example you can try for the capture.
> 
> 
> ```
> ...



You are right!:e:e


----------

