Nginx日志切割
nginx配置文件
cat /app/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
reopen
这里只有access、error日志,如果日志还有其他的,根据需求改脚本即可
cat <<EOF>/scripts/nginx-log.sh
#!/bin/bash
NGINX_PID=`cat /app/nginx/logs/nginx.pid`
LOG_DIR="/app/nginx/logs"
# 将旧日志备份
for i in `ls $LOG_DIR`
do
if [ ${i#*.} == "log" ]; then
mv $LOG_DIR/$i ${i}-$(date +%F -d 'yesterday')
fi
done
# 生成新日志
ps -aux|grep nginx|grep -v nginx-log.sh|grep -v color &>/dev/null
if [ $? -eq 0 ]; then
kill -USR1 $NGINX_PID
fi
EOF
将脚本放入cron中定期执行
# 给与x权限
chmod a+x /scripts/nginx-log.sh
# 创建定期任务
cat <<EOF>/var/spool/cron/root
0 0 * * 0 bash /scripts/nginx-log.sh
EOF
logrotate
cat <<EOF>/etc/logrotate.d/nginx
/app/nginx/logs/*.log {
weekly
rotate 4
compress
nodelaycompress
ifempty
create 0440 root root
dateext
postrotate
[ ! -f /app/nginx/logs/nginx.pid ] || kill -USR1 `cat /app/nginx/logs/nginx.pid
endscript
}
EOF
cat <<EOF>/var/spool/cron/root
0 0 * * 0 /usr/sbin/logrotate -f /etc/logrotate.d/nginx &>/dev/null
EOF
logrotate 的默认配置文件是 /etc/logrotate.conf。主要参数:
daily指定转储周期为每天
weekly指定转储周期为每周
monthly指定转储周期为每月
dateext在文件末尾添加当前日期
dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数
compress通过gzip 压缩转储以后的日志
nocompress不需要压缩时,用这个参数
copytruncate先把日志内容复制到旧日志文件后才清除日志文件内容,可以保证日志记录的连续性
nocopytruncate备份日志文件但是不截断
create mode owner group转储文件,使用指定的文件模式创建新的日志文件
nocreate不建立新的日志文件
delaycompress和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress覆盖 delaycompress 选项,转储同时压缩。
errors address专储时的错误信息发送到指定的Email 地址
ifempty即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty如果是空文件的话,不转储
mail address把转储的日志文件发送到指定的E-mail 地址
nomail转储时不发送日志文件
olddir directory转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir转储后的日志文件和当前日志文件放在同一个目录下
missingok: 在日志轮循期间,任何错误将被忽略,例如 “文件无法找到” 之类的错误
rotate count指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行