nginx:如果不存在则创建目录

标签 nginx

我是 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/

相关文章:

php `$_SERVER[' PHP_SELF']` 为空

nginx - 如何使用Nginx在特定位置路径上提供静态文件目录?

nginx - phpMyAdmin和nginx配置,获取ip后的phpmyadmin

TCPing 时 Azure VM 端口关闭

ssl - 从 Kubernetes Nginx Ingress 中的 ssl 重定向中排除特定主机

nginx - Nginx中Golang App的基本配置

ssl - 无法从禁用了 SSLv2、v3 和 TLSv1.0 的站点中提取 RSS 源

amazon-web-services - AWS Elastic Beanstalk 仅对 API 网关开放

php - 有什么方法可以通过本地网址(例如http://mydomain.dev)访问docker(nginx)容器?

wordpress - Nginx WordPress 多站点重写子目录安装规则