Nginx 访问日志文件路径



这是我的default.conf。我指定访问.log和错误.log路径。我使用 docker 来构建它。

server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

if ($time_iso8601 ~ "^(d{4})-(d{2})-(d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
access_log /var/log/nginx/test/access-$year-$month-$day.log;
error_log /var/log/nginx/test2/error.log;
sendfile off;
client_max_body_size 100m;
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /.ht {
deny all;
}
}

当我建造它并向上时。我收到了错误消息。

nginx_1  | nginx: [emerg] open() "/var/log/nginx/test2/error.log" failed (2: No such file or directory)

如何自动生成文件路径?这是我的docker-compose,Github

您遇到的错误是因为test2目录不存在。Nginx不会自动为您创建此功能。所以你需要在你的Dockerfile中使用以下命令创建它

RUN mkdir -p /var/log/nginx/test2 /var/log/nginx/test

最新更新