我是 nginx 的新手,我有给定的 nginx 配置。 有一个像这样的映射:
map $http_host $my_customer {
default "default";
"~*cust1" "cust1";
"~*cust2" "cust2";
}
还有 access_log 行:
access_log /my/log/path/access.log
现在我想为每个客户提供单独的日志目录和日志文件,所以我将 access_log 行更改为:
access_log /my/log/path/$my_customer/access.log
如果 $my_customer
目录已经存在,这会很好地工作。但是如果不存在,那么nginx就不会记录。我知道如何检查目录是否存在:
if (!-d /my/log/path/$my_customer) {}
但是如何在 nginx 配置文件中创建一个目录呢?
最佳答案
为了启动 nginx 进程,必须提前创建所有目录。
dir的所有者应该是nginx配置文件(默认为/etc/nginx/nginx.conf
)定义的worker进程使用的用户。
用户应该对该目录具有写入权限。
正如@Alexey Ten 所注意到的,使用默认日志位置是一种很好的做法:
/var/log/nginx/$my_customer.access.log
否则,你必须做这样的事情:
mkdir -p -m 755/my/log/path/$my_customer
关于nginx:如果不存在则创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34995385/