0
点赞
收藏
分享

微信扫一扫

Nginx日志如何按天分割?

南陵王梁枫 2022-01-10 阅读 86
nginx运维

转自:微点阅读  https://www.weidianyuedu.com

nginx 日志分割是比较常见的运维工作,关于这方面的文章也很多,通常无外乎两种做法:

1.cron定期执行shell脚本对日志文件进行归档。2.使用专门日志归档logrotate。

以上方式与nginx其实没有特别的关系。从nginx 0.7.6 版本开始,access_log 的路径配置可以包含变量,我们以此进行日志分割。

同时我们基于nginx的 timeiso8601 内嵌变量来获取时间。time_iso8601格式如下:

2018-09-21T16:01:02+02:00

然后使用正则表达式来获取所需时间的数据。

http {  log_format default_format "$remote_addr - $remote_user [$time_iso8601] "$request" "      "$status $body_bytes_sent "$http_referer" "      ""$http_user_agent" "$http_x_forwarded_for" "$request_body"";      server {          listen 443 ssl;          ... ...          if ($time_iso8601 ~ "(\d{4}-\d{2}-\d{2})") {            set $tttt $1;          }          access_log /log/blog_access_$tttt.log;      }}

主要关注两个地方:

1.要在外方法的log_format上添加 $time_iso8601 ,将原来的time_local修改为time_iso8601。2.在单个server中,通过正则表达式截取 $time_iso8601 生成时间戳。

最后插一个话题,你知道 $time_iso8601 为什么叫这个名字吗?

举报

相关推荐

0 条评论