Nginx and php log segmentation

Source: Internet
Author: User
Author: zhanhailiang date: by default, nginx will generate only one accesslog and one errorlog, and the log file will become very large as it accumulates every day. if you need to analyze the log, whether using script analysis or downloading logs for local analysis,
Author: zhanhailiang date:

By default, nginx generates only one access. log and an error. logs are accumulated every day, and the log files become very large. if you need to analyze the logs, whether using script analysis or downloading the logs for local analysis, it is not convenient. This is also required for php logs.


Therefore, splitting access logs and error logs every day is conducive to log analysis.

#! /Bin/sh # for daily scheduled splitting, you can select the corresponding version for low-end users to run ngPid = '/usr/local/nginx/logs/nginx. pid 'phpfpmpid = '/usr/local/php/var/run/php-fpm.pid' ngLogs = '/usr/local/nginx/logs' # access. log error. logphpLogs = '/usr/local/php/log' # errors. log slow. logsaveLog = '/home/service/logs' top = $ saveLog/$ (date + % Y)/$ (date + % m) mkdir-p $ top/nginxif [-f $ ngLogs/access. log]; then mv $ ngLogs/access. log $ top/nginx/access. $ (date + % Y % m % d ). logfi if [-f $ ngLogs/error. log]; then mv $ ngLogs/error. log $ top/nginx/error. $ (date + % Y % m % d ). logfi echo 'cat $ ngPid 'kill-USR1 'cat $ ngPid' # repeat the log file Reopen the log files mkdir-p $ top/phpif [-f $ phpLogs/errors. log]; then mv $ phpLogs/errors. log $ top/php/errors. $ (date + % Y % m % d ). logfi if [-f $ phpLogs/php-fpm.log]; then mv $ phpLogs/php-fpm.log $ top/php-fpm. $ (date + % Y % m % d ). logfi echo 'cat $ phpfpmpid' kill-USR1 'cat $ phpfpmpid' # Restart the php log Reopen the log files chmod-R 777 $ top

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.