In centos nginx automatically splits access logs by date

  • 2020-05-24 06:45:42
  • OfStack

The Web access log (access_log) records the access behavior of all external clients to the Web server, including important information such as the client IP, access date, access URL resources, HTTP status code returned by the server and so on.

A typical Web access log is as follows:


192.168.50.195 - - [17/Jun/2016:23:59:12 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36" "-"1

1. To solve problems:

When the site visits large, the log data will be a lot, if all written to 1 log file, the file will become larger and larger. File speed will slow down, such as a file of a few hundred megabytes. Writing to the log can affect the operation speed. Also, if I want to look at the access log, a file of a few hundred megabytes is slow to download and open.

Note: you can upload your nginx, apache, and iis log files to help you analyze the security aspects of your site using the free 3rd party log analysis tool, logpro. Specialized, after all, is more professional. Logger also limits the size of uploaded files to no more than 50m.

2, automatic log cutting script writing

nignx has no automatic mechanism for storing logs in separate files. Because nginx doesn't automatically save files for you. Therefore, you need to write your own script to implement it.


#!/bin/bash
# Program:
#   Auto cut nginx log script.
# 2016/6/15 luozhibo 

# nginx Log path  /var/log/nginx/
LOGS_PATH=/var/log/nginx
TODAY=$(date -d 'today' +%Y-%m-%d)
#echo $TODAY

#  Move the log and rename it 
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${TODAY}.log
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${TODAY}.log

#  to nginx The main process sends a signal to reopen the log file 
kill -USR1 $(cat /var/run/nginx.pid)

The shell script above works like this:

Move and rename previous log files for backup purposes.

3. crontab automatic task configuration

Write vim /etc/crontab directly or write automated tasks directly from echo


echo '59 23 * * * root /var/log/nginx/nginx_log_division.sh >> /var/log/nginx/cutnginxlog.log 2>&1' >> /etc/crontab

Automatically execute scheduled tasks at 23:59 p.m. every night. Run as an root user and write the execution logs (error and correct logs) of the automated tasks to the cutnginxlog.log "command > > 2 > & 1 "means to append both correct and error output to the same file


Related articles: