Automatic log cutting generated by nginx on the CentOS server

Source: Internet
Author: User
1. edit the shell program for log cutting. set the directory to [plain] viewplaincopy # vi/data/nginx/cut_nginx_log.sh and enter the code: [python] viewplaincopy #! /Bin/bash # Thisscriptrunat00: 00 functioncutAccess () {di

1. edit the shell program for log cutting and customize the Directory


[Plain]View plaincopy
  1. # Vi/data/nginx/cut_nginx_log.sh

Enter the code:


[Python]View plaincopy
  1. #! /Bin/bash

  2. # This script run at 00:00

  3. Function cutAccess ()

  4. {

  5. Dir = $1

  6. Newdir = "$ {dir}/$ (date-d" yesterday "+" % Y ")/$ (date-d" yesterday "+" % m ")"

  7. Suffix = $ (date-d "yesterday" + "% Y % m % d ")

  8. Mkdir-p $ newdir

  9. Mv $ {dir}/access. log $ {newdir}/access. $ suffix. log

  10. }

  11. CutAccess "/home/wwwlogs/www.yourdomain.com /"

  12. CutAccess "/home/wwwlogs/www.yourdomain-1.com /"

  13. CutAccess "/home/wwwlogs/www.yourdomain-2.com /"

  14. # Restart nginx

  15. Kill-HUP 'cat/usr/local/nginx/logs/nginx. pid'



2. add a scheduled task to automatically cut at every day


[Plain]View plaincopy
  1. # Crontab-e

  2. 0 0 ***/bin/bash/data/nginx/cut_nginx_log.sh


3. nginx log format



[Plain]View plaincopy
  1. Log_format access' $ remote_addr-$ remote_user [$ time_local] "$ request "'

  2. '$ Status $ body_bytes_sent "$ http_referer "'

  3. '"$ Http_user_agent" $ http_x_forwarded_for ';

  4. Access_log/home/wwwlogs/www.yourdomain.com/access.log access;


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.