# Using dynamic access\_log, automatically create parent directory

**URL:** <https://www.ruby-forum.com/t/using-dynamic-access-log-automatically-create-parent-directory/242788>\
**Category:** NGINX\
**Created:** [July 27, 2015, 4:49am UTC](https://www.ruby-forum.com/t/using-dynamic-access-log-automatically-create-parent-directory/242788 "2015-07-27T04:49:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [July 27, 2015, 4:49am UTC](https://www.ruby-forum.com/t/using-dynamic-access-log-automatically-create-parent-directory/242788/1 "2015-07-27T04:49:43Z")

</div>

We use a dynamic value for access logs:

```
access_log /var/log/nginx/domains/$host/access.log main;

```

However, if the $host directory does not exist in /var/log/nginx/domains  
nginx fails with an error creating the access log. Is there a way to  
have  
nginx create the $host directory automatically instead of failing?

Seems like this should be default behavior?

Posted at Nginx Forum:

> **[Using dynamic access\_log, automatically create parent directory](https://forum.nginx.org/read.php?2,260563,260563#msg-260563)**
>
> We use a dynamic value for access logs: access\_log /var/log/nginx/domains/$host/access.log main; However, if the $host directory does not exist in /var/log/nginx/domains nginx fails with an error creating the access log. Is there a way to have nginx...

---

<div class="post-metadata">

**Author:** ![justink101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@justink101](https://www.ruby-forum.com/u/justink101)\
**Post date:** [July 29, 2015, 12:46am UTC](https://www.ruby-forum.com/t/using-dynamic-access-log-automatically-create-parent-directory/242788/2 "2015-07-29T00:46:00Z")

</div>

On Sun, Jul 26, 2015 at 10:49:01PM -0400, justink101 wrote:

Hi there,

> We use a dynamic value for access logs:
> 
> ```
> access_log /var/log/nginx/domains/$host/access.log main;
> 
> ```
> 
> However, if the $host directory does not exist in /var/log/nginx/domains  
> nginx fails with an error creating the access log. Is there a way to have  
> nginx create the $host directory automatically instead of failing?

Not in stock nginx.

You could create a patch to do it; but I suspect that the overhead of  
an extra “mkdir -p”-like call for every log line written would not be  
worth it.

If you are willing to rearrange your log file structure such that the  
variable part is in the filename, and not a parent directory name,  
then stock nginx can help you.

> Seems like this should be default behavior?

I think “no”.

But it’s the person who writes the code who has the opinion that  
matters;  
at least for their local version of nginx.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
