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分开始执行;
相关文章
相关标签/搜索