nginx日志按照日期切割.md
利用nginx中的内置变量,可以很方便的将nginx日志按照日期切割,再也不需要重启nginx来切割日志了。
代码如下
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
map $time_iso8601 $logdate {
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
access_log logs/access-$logdate.log main;
open_log_file_cache max=10;
当然也可以有更加细粒度的时间片,如下
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
set $minutes $5;
set $seconds $6;
}
参考资料:
https://jingsam.github.io/2019/01/15/nginx-access-log.html
转载请注明:IPCPU-网络之路 » nginx日志按照日期切割