这里主要介绍nginx日志切割.(访问日志与错误日志)
准备好一台机器,配置随意,安装nginx应用。
1. nginx安装步骤
# tar xf nginx-1.9.4.tar.gz # cd nginx-1.9.4 # yum install openssl-devel pcre pcre-devel –y # useradd -r -s /sbin/nologin nginx # ./configure --user=nginx--group=nginx--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream && make && make install
2. nginx配置文件
user nginx nginx; worker_processes 2; error_log logs/error/error.log info; pid logs/nginx.pid; worker_cpu_affinity 00000001 00000010; events { use epoll; worker_connections 40960; multi_accept on; } http { include mime.types; default_type application/octet-stream; log_format main '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] ' '$upstream_response_time $request_time ' '$http_host $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_accept_language" "$http_user_agent" '; access_log logs/access/access.log main; sendfile on; keepalive_timeout 65; server_tokens off; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; server { listen 80; server_name localhost; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
3. 编写nginx切割脚本
# vim nginx_logcut
#!/bin/bash #description:cut nginx log per day. #auth:Net20 huangxiag #define logs dir LOGS_ACCESS="/usr/local/nginx/logs/access" LOGS_ERROR="/usr/local/nginx/logs/error" #define pid file PID_PATH="/usr/local/nginx/logs/nginx.pid" #define date DATE=`date -d "yesterday" +%F` DATE_DIR=`date +%Y-%m` #cut log if [ ! -d ${LOGS_ACCESS}/${DATE_DIR} ];then mkdir ${LOGS_ACCESS}/${DATE_DIR} fi mv ${LOGS_ACCESS}/access.log ${LOGS_ACCESS}/${DATE_DIR}/access_$DATE.log if [ ! -d ${LOGS_ERROR}/${DATE_DIR} ];then mkdir ${LOGS_ERROR}/${DATE_DIR} fi mv ${LOGS_ERROR}/error.log ${LOGS_ERROR}/${DATE_DIR}/error_$DATE.log #reload nginx kill -USR1 `cat ${PID_PATH}`
4. 编写日志打包脚本
# vim nginx_logtar.sh
#!/bin/bash #description:tar nginx log per month &drop six month ago log. #auth:Net20 huangxiang #define logs dir LOGS_ACCESS="/usr/local/nginx/logs/access" LOGS_ERROR="/usr/local/nginx/logs/error" #define date DATE_DIR=`date -d "-1 month" +%Y-%m` DATE_SIX=`date -d "-6 month" +%Y-%m` #tar nginx log and drop six month ago log. if [ -d ${LOGS_ACCESS}/${DATE_DIR} ];then cd ${LOGS_ACCESS} tar -czvPf ${DATE_DIR}_log.tar.gz ${DATE_DIR} rm -fv ${DATE_DIR}/* rm -fv ${DATE_SIX}_log.tar.gz else echo "Last month access_log_dir was not exit." fi if [ -d ${LOGS_ERROR}/${DATE_DIR} ];then cd ${LOGS_ERROR} tar -czvPf ${DATE_DIR}_log.tar.gz ${DATE_DIR} rm -fv ${DATE_DIR}/* rm -fv ${DATE_SIX}_log.tar.gz else echo "Last month error_log_dir was not exit." fi
5. 编写定时任务计划
脚本都放在/root/bin下面(你们随意放,后面的定时任务别写错位置就好)
脚本加执行权限。
# crontab -l
00 00 * * * bash /root/bin/nginx_logcut.sh > /tmp/nginx_logcut.cron.log 2> /tmp/nginx_logcut.cron.err 00 00 1 * * bash /root/bin/nginx_logtar.sh > /tmp/nginx_logtar.cron.log 2> /tmp/nginx_logtar.cron.err
6. 调整电脑时间
调整操作系统时间为23:59:00
记得访问下80,让nginx出日志。(还有随便访问一个404页面)
7. 测试
凌晨之后,查看是否已经日志切割。
这里没给出具体测试结果,你们自己去整整。最主要的脚本已经给出。
原创文章,作者:Net20_赤羽,如若转载,请注明出处:http://www.178linux.com/23380