nginx log management - log cutting, custom log fields, clean up logs

Server nginx log management

After using nginx to deploy the site on the server, the nginx log is one of the important means for us to monitor the site and troubleshoot online problems. Reasonable management of logs, there are methods such as cutting logs at regular intervals, cleaning logs after archiving, and customizing the log fields that need to be recorded.

custom log field

In the default configuration of nginx, there is a format called main log, which contains many fields, some of which may not be the information we want to know, and can be deleted or added. For example, a log rule for my_blog is configured as follows.

http{
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  my_blog '$remote_addr,$time_iso8601,$request,'
                      '$http_referer,$http_x_forwarded_for';
    access_log  /var/log/nginx/access.log  my_blog;
}
# nginx日志修改时间格式为年月日时分秒
$time_iso8601    生成格式:2019-04-20T09:24:35+08:00
$time_local          生成格式: 20/Apr/2019:09:24:13 +0800

nginx cleanup log

Rename the log file through mv for backup, the original log file will no longer exist, and re-opening nginx to load the log file will generate a new log file

$ mv /var/log/nginx/access.log /var/log/nginx/access.log.bak  # 重命名日志文件
$ nginx -s reopen
# 重新加载日志文件

# 还是上面那个例子,把 my_blog 的日志通过重命名进行备份,日志文件也就清理掉了
$ mv /www/wwwlogs/my_blog.log /www/wwwlogs/my_blog.log.bak

For more details, please refer to
nginx log detailed explanation

Log configuration and cutting

Timed task completes log cutting

cover picture
nginx log management - log cutting, custom log fields, clean log cover

Guess you like

Origin blog.csdn.net/zhouweihua138/article/details/129655138