search

Home  >  Q&A  >  body text

How to do log cutting in nginx

Nginx logs are written in a file. I need one Nginx Log every day
I saw many methods on the Internet, all of which are to write scripts and cut Log files in a fixed format
Can't Nginx do log cutting by itself?

高洛峰高洛峰2781 days ago618

reply all(5)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 17:31:36

    Confirm cron is running

    service crond status

    Modify configuration file

    vi /etc/crontab

    Confirm scheduled tasks

    vi /etc/cron.daily/logrotate

    Write logrotate configuration file vi /etc/logrotate.d/nginx

    /var/log/nginx/*.log {
            #指定转储周期为每天
            daily
            missingok
            #指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
            rotate 5
            #compress
            #delaycompress
            #如果是空文件的话,不转储
            notifempty
            #create 640 root adm
            sharedscripts
            postrotate
                    [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
            endscript
    }

    Test configuration

    /usr/sbin/logrotate -f /etc/logrotate.d/nginx

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 17:31:36

    The logging needs of system administrators are ever-changing. For the sake of its lightweight, Nginx really does not worry about log management.

    Official LogRotation Wiki Page

    I guess you have also found it, you can use your own script or logrotate.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 17:31:36

    nginx log files do not have a rotate function. If you don't handle it, the log file will become larger and larger. Fortunately, we can write an nginx log cutting script to automatically cut the log file.

    The first step is to rename the log file. You don’t have to worry about nginx not being able to find the log file and losing the log after renaming. Before you reopen the log file with the original name, nginx will still write logs to the file you renamed. Linux relies on file descriptors rather than file names to locate files.

    The second step is to send the USR1 signal to the nginx main process.

    After receiving the signal, the main process of nginx will read the log file name from the configuration file, reopen the log file (named with the log name in the configuration file), and use the user of the worker process as the owner of the log file.

    After reopening the log file, the nginx main process will close the log file with the same name and notify the worker process to use the newly opened log file.

    The worker process immediately opens new log files and closes log files with the same name.

    Then you can process the old log files.

    nginx log automatic cutting script by date is as follows

    • nginx log cutting script
    • author: http://www.nginx.cn
    • !/bin/bash
    • Set the log file storage directory

    logs_path="/usr/local/nginx/logs/"

    • Set pid file

    pid_path="/usr/local/nginx/nginx.pid"

    • Rename log file

    mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log

    • Signal the nginx main process to reopen the log

    kill -USR1 cat ${pid_path}

    Save the above script nginx_log.sh, or click here to download

    crontab setup job

    0 0 * * * bash /usr/local/nginx/nginx_log.sh
    This will rename the nginx log to date format at 0:00 every day, and regenerate today's new log file.

    Detailed source reference: http://www.nginx.cn/255.html

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 17:31:36

    The easiest way is to configure it directly in the configuration file:

    if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
        set $year ;
        set $month ;
        set $day ;
    }
    access_log  /home/wwwlogs/$year-$month-$day-bbs-access.log access;

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 17:31:36

    This method often generates files without dates and has been deprecated

    reply
    0
  • Cancelreply