In centos, nginx Automatically splits access logs by date
The Web access log (access_log) records the access behaviors of all external clients to the Web server, including the Client IP address, access date, accessed URL resources, HTTP status codes returned by the server, and other important information.
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. Solve the problem:
When the website traffic is high, there will be a lot of log data. If all the logs are written into one log file, the file will become larger and larger. A large file speed slows down, for example, a file may be several hundred megabytes. The operation speed is affected when logs are written. In addition, if I want to see the access log, a file of several hundred megabytes can be downloaded and opened slowly.
Note: You can use the free third-party log analysis tool logstore to upload nginx, apache, and iis log files, which help you analyze website security. After all, they are specialized and more professional. Logstore also limits the size of uploaded files to a maximum of 50 MB.
2. Write an Automatic Log cutting script
There is no automatic Separation Mechanism for file storage logs in the network. Because nginx does not automatically save the file. Therefore, you need to write your own scripts to implement them.
#! /Bin/bash # Program: # Auto cut nginx log script. #2016/6/15 luozhibo # nginx log Path/var/log/nginx/LOGS_PATH =/var/log/nginxTODAY = $ (date-d 'today' + % Y-% m-% d) # echo $ TODAY # Move the log and rename it mv $ {LOGS_PATH}/error. log $ {LOGS_PATH}/error _ $ {TODAY }. logmv $ {LOGS_PATH}/access. log $ {LOGS_PATH}/access _ $ {TODAY }. log # send the signal kill-USR1 $ (cat/var/run/nginx. pid)
The principle of the above shell script is:
First, move and rename the previous log file to back up the file.
3. crontab automatic task Configuration
Directly write vim/etc/crontab or directly write automatic tasks through echo
echo '59 23 * * * root /var/log/nginx/nginx_log_division.sh >> /var/log/nginx/cutnginxlog.log 2>&1' >> /etc/crontab
The Automatic Execution Plan Task runs as the root user at every night and automatically writes the execution logs (errors and correct logs) of the automatic task to the cutnginxlog. log "command> 2> & 1" indicates to append the correct output and error output to the same file.
4. Reference Links:
Nginx automatic access log Cutting
Store nginx logs by date as the file name