nginx日誌自動切割

1.日誌配置(Nginx 日誌)nginx

access.log----記錄哪些用戶,哪些頁面以及用戶瀏覽器,IP等訪問信息;
error.log------記錄服務器錯誤的日誌

 

#配置日誌存儲路徑:
location / {
         access_log          /usr/local/nginx/logs/access.log;
         error_log           /usr/local/nginx/logs/error.log;
}

按本身要求配置日誌格式:shell

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  60;
    include  /usr/local/nginx/vhost/*.conf;
    log_format main '$remote_addr -$remote_user [$time_local] "request"'
                    '$status $body_bytes_sent "$http_referer"'
                    '"$http_user_agent" "$http_x_forwarded_for"'
                    '"$gzip_ratio" $request_time $request_length' ;
    open_log_file_cache max=1000 inactive=60s;
}

    操做完上面的,日誌就按本身的要求格式存儲在指定位置瀏覽器

2.日誌切割(按天進行日誌切割)bash

   A.編寫腳本服務器

#!/bin/bash
year=`date +%Y`
month=`date +%m`
day=`date +%d`
logs_backup_path="/usr/local/nginx/logs_backup/$year$month"               #日誌存儲路徑

logs_path="/usr/local/nginx/logs/"                                                             #要切割的日誌路徑
logs_access="access"                                                                            #要切割的日誌
logs_error="error"
pid_path="/usr/local/nginx/logs/nginx.pid"                                                 #nginx的pid

[ -d $logs_backup_path ]||mkdir -p $logs_backup_path
rq=`date +%Y%m%d`
#mv ${logs_path}${logs_access}.log ${logs_backup_path}/${logs_access}_${rq}.log
mv ${logs_path}${logs_error}.log ${logs_backup_path}/${logs_error}_${rq}.log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

3.作定時任務app

crontab –e
59 23 * * * bash /usr/local/nginx/shell/cut_ngnix_log.sh   #天天23:59分開始執行;
相關文章
相關標籤/搜索