更新时间:2022-09-15 12:28:30
直接在nginx配置文件中,配置日志循环,而不需使用logrotate或配置cron任务。需要使用到$time_iso8601 内嵌变量来获取时间。$time_iso8601格式如下:2015-08-07T18:12:02+02:00。然后使用正则表达式来获取所需时间的数据。
使用下面的代码块
1
2
3
4
5
6
7
|
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})" ) {
set $year $ 1 ;
set $month $ 2 ;
set $day $ 3 ;
} access_log /data/logs/nginx/www.ttlsa.com-$year-$month-$day-access.log; |
也可以使用Perl语法来捕获,如下:
1
2
3
|
if ($time_iso8601 ~ "^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})" ) {}
access_log /data/logs/nginx/www.ttlsa.com-$year-$month-$day-access.log; |
1
2
3
4
5
6
7
8
9
|
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 ;
} |
非常方便的进行日志分割。建议按小时分割日志,方便分析查询日志。
1
2
3
4
5
6
7
8
9
10
|
server { listen 80 ;
server_name www.test.com;
if ($time_iso8601 ~ "^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})" ) {}
access_log / var /log/nginx/www.test.com-$year-$month-$day-access.log;
location / {
root html;
index index.html index.htm;
}
}
|