1.日志配置
Nginx 日志分
access.log 记录哪些用户,哪些页面以及用户浏览器,IP等访问信息;
error.log 记录服务器错误的日志
配置日志存储路径
location / {
access_log /usr/local/nginx/logs/access.log;
error_log /usr/local/nginx/logs/error.log;
}按自己要求配置日志格式
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 60;
include /usr/local/nginx/vhost/*.conf;
#access 日志格式设置
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.日志切割(按天进行日志切割)
A.编写脚本。
注意:在 linux 系统里面直接 vim 打开文件编写,不要在 win 电脑上编辑好,上传上去,会执行不了。
#!/bin/bash
year=`date +%Y`
month=`date +%m`
day=`date +%d`
logs=/check/log/cut_pingce_shell.log
echo -e "\n$(date '+%Y-%m-%d %H:%M:%S') start " >> $logs
#日志存储路径
logs_backup_path="/mnt/log/nginx/pingce/$year$month"
#要切割的日志路径
logs_path="/mnt/log/nginx/pingce/"
#要切割的日志
logs_access="pingce_access"
logs_error="pingce_error"
#nginx的pid
pid_path="/usr/local/nginx/logs/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
#向nginx主进程发送USR1信号,重新打开日志文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
#重新加载 nginx 配置,防止不会写日志
/etc/init.d/nginx reload
echo "$(date '+%Y-%m-%d %H:%M:%S') done" >> $logs3.做定时任务
crontab –e
59 23 * * * bash /usr/local/nginx/shell/cut_ngnix_log.sh #每天23:59分开始执行;