這里主要介紹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.www58058.com/23380