Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: create directory if it doesn't exist

Tags:

nginx

I'm new to nginx and I have a given nginx config. There is a mapping like:

map $http_host $my_customer {
  default "default";
  "~*cust1" "cust1";
  "~*cust2" "cust2";
}

And there is the access_log line:

access_log /my/log/path/access.log

Now I want to have separate log-directories and log-files for each customer, so I changed the access_log line into:

access_log /my/log/path/$my_customer/access.log

This works fine if the $my_customer-directory already exists. But if it doesn't exist, then nginx does not log. I know how I can check if the directory exists:

if (!-d /my/log/path/$my_customer) {}

But how is it possible to create a directory inside the nginx config file?

like image 847
Munchkin Avatar asked Nov 03 '25 16:11

Munchkin


1 Answers

In order to start nginx process all directories have to be created in advance.
The owner of dir should be the user used by worker processes defined in nginx configuration file (/etc/nginx/nginx.conf by default).
The user should have write permissions to this directory.

As @Alexey Ten noticed, it is a good practice to use default logs location: /var/log/nginx/$my_customer.access.log

Otherwise, you have to do something like that:
mkdir -p -m 755 /my/log/path/$my_customer

like image 74
antonbormotov Avatar answered Nov 05 '25 05:11

antonbormotov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!