这个需求可以借助定时任务或者第三方软件来实现,但我想简单一点。
可以在 http 模块中使用 map 指令定义变量:
map $time_iso8601 $year { default '0000'; "~^(\d{4})-(\d{2})-(\d{2})" $1; } map $time_iso8601 $month { default '00'; "~^(\d{4})-(\d{2})-(\d{2})" $2; } map $time_iso8601 $day { default '00'; "~^(\d{4})-(\d{2})-(\d{2})" $3; }
然后在 server 模块中使用:
access_log /var/log/nginx/foo.com/access-$year$month$day.log bar;
注意,仅限 access_log 中使用,error_log 中不可以使用。而且,在 access_log 中使用变量时,必须定义 root 为一个存在的目录!