Nginx automatically splits access logs by date in CentOS

Source: Internet
Author: User
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, the access date, the URL resource accessed, the 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"3040"-""Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36""-"

1, to solve the problem:

When the amount of website access is large, the log data will be many, if all write to a log file, the file will become more and more large. The file will slow down at a large speed, such as a file hundreds of trillion. When the log is written, it affects the speed of operation. Also, if I want to see an access log, a hundreds of trillion file, download it down and open it very slowly.

Note: Use the third party free Log analysis tool-log treasure, you can upload Nginx, Apache, IIS log files, they help analyze the security aspects of the site. After all, specialization, more professional. Log treasure to upload the file is also made a size limit, not more than 50m.

2, log automatic cutting script writing

NIGNX does not have a mechanism to automatically separate file storage logs. Because Nginx it will not help you automatically save files. Therefore, you need to write your own script to implement.

#!/bin/bash# Program:#     Auto cut nginx log script.# 2016/6/15  luozhibo # nginx日志路径 /var/log/nginx/-d'today' +%Y-%m-%d)#echo $TODAY# 移动日志并改名${LOGS_PATH}${LOGS_PATH}/error_${TODAY}${LOGS_PATH}${LOGS_PATH}/access_${TODAY}.log# 向nginx主进程发送重新打开日志文件的信号kill -USR1 $(cat /var/run/nginx.pid)

The rationale for the above shell script is:

Rename the previous log files first, and the purpose is to backup.

3, crontab automatic task configuration

Write Vim/etc/crontab directly or write automatic tasks directly via Echo

echo '5923 * * * root /var/log/nginx/nginx_log_division.sh >> /var/log/nginx/cutnginxlog.log2>&1' >> /etc/crontab

Automatically perform scheduled tasks every 23:59 minutes
Run as Root user
Automatically writes the execution log (error and correct log) of the automatic task to Cutnginxlog.log
"Command >> 2>&1" means that the correct output and error output are saved to the same file in an append way

4. Reference Links:

Nginx Automatic Cutting access log

Storing Nginx logs by date as file name

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the CentOS by date automatic segmentation access log, including the contents of the content, I hope that the PHP tutorial interested friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.