nginx日志切割/分割之按天生成及定期刪除日志
引言
問題:nginx會(huì)按照nginx.conf的配置生成access.log和error.log,隨著訪問量的增長,日志文件會(huì)越來越大,既會(huì)影響訪問的速度(寫入日志時(shí)間延長),也會(huì)增加查找日志的難度,nginx沒有這種按天或更細(xì)粒度生成日志的機(jī)制。所以下面介紹三種方法:
- 1.Nginx內(nèi)部配置設(shè)置日志文件格式。(推薦方法1,map方式)
- 2.寫腳本,通過定時(shí)任務(wù)按天重命名日志、重啟nginx的方法實(shí)現(xiàn)(有重啟失敗的風(fēng)險(xiǎn))
- 3.通過工具cronolog實(shí)現(xiàn)。
方法1
需要使用到 timeiso8601內(nèi)嵌變量來獲取時(shí)間。 time_iso8601 內(nèi)嵌變量來獲取時(shí)間。time iso8601內(nèi)嵌變量來獲取時(shí)間。time_iso8601格式如下:2018-09-21T16:01:02+02:00。然后使用正則表達(dá)式來獲取所需時(shí)間的數(shù)據(jù)。
按天分割日志
配置在server段:
注意層次關(guān)系,這段腳本一定要加到server配置內(nèi)部,且if要在access_log前面,否則set的變量將無法引用
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") { ? ? ? ? set $year $1; ? ? ? ? set $month $2; ? ? ? ? set $day $3; } ?access_log ?/var/logs/xxxx/access/xxxxx_xx_access_$year-$month-$day.log ?main;
查看日志是否生成:
[xx@xxx access]# ll xxxxx_xx_access_2018-09-21.log -rw-r--r-- 1 root root 408848 Sep 21 16:01 xxxxx_xx_access_2018-09-21.log
按小時(shí)、分、秒分割:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") { ? ? set $year $1; ? ? set $month $2; ? ? set $day $3; ? ? set $hour $4; ? ? set $minutes $5; ? ? set $seconds $6; } access_log ?/var/logs/xxxx/access/xxxxx_xx_access_$year-$month-$day-hour-minutes-seconds.log ?main;
在必要的時(shí)候可以按小時(shí)分割,方便日志分析。
上面的方法有兩個(gè)問題:
- 一是如果
if
條件不成立,那么$year
、$month
和$month
這三個(gè)變量將不會(huì)被設(shè)置,那么日志將會(huì)記錄到access-$year-$month-$day.log
這個(gè)文件中; - 二是if只能出現(xiàn)在server和location塊中,而access_log通常會(huì)配置到頂層的http塊中,這時(shí)候if就不適用。
如果要在http
塊中設(shè)置access_log
,更好的方法是使用map
指令:
map $time_iso8601 $logdate { '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd; default 'date-not-found'; } access_log logs/access-$logdate.log main;
map指令通過設(shè)置默認(rèn)值,保證$logdate始終有值,并且可以出現(xiàn)在http塊中,完美地解決了if指令的問題。
最后,為了提高日志的效率,建議配置open_log_file_cache,完整的日志分割配置如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; map $time_iso8601 $logdate { '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd; default 'date-not-found'; } access_log logs/access-$logdate.log main; open_log_file_cache max=10;
但是缺點(diǎn):每次請(qǐng)求都會(huì)進(jìn)行map的正則表達(dá)式匹配,對(duì)性能有影響。
方法2
2:創(chuàng)建分割日志文件的腳本,添加定時(shí)任務(wù)
2.1.1寫腳本:重命名日志文件、重啟nginx
例如存放路徑:/usr/local/nginx/sbin/cut_nginx_logs.sh,按天分割具體內(nèi)容:
#!/bin/bash #function:cut nginx log files #set the path to nginx log files log_files_path="/data/nginxlog/" log_files_dir=${log_files_path} #set nginx log files you want to cut log_files_name=(access ) #set the path to nginx. nginx_sbin="/usr/local/nginx/sbin/nginx" #Set how long you want to save save_days=30 ############################################ #Please do not modify the following script # ############################################ #mkdir -p $log_files_dir log_files_num=${#log_files_name[@]} #cut nginx log files for((i=0;i<$log_files_num;i++));do mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}${log_files_name[i]}.log_$(date -d "yesterday" +"%Y-%m-%d") done #delete 30 days ago nginx log files find $log_files_path -mtime +$save_days -exec rm -rf {} \;? #restart nginx $nginx_sbin -s reload
2.1.2.使用crontab添加定時(shí)任務(wù)
//打開定時(shí)任務(wù) crontab -e //進(jìn)入編輯模式 i //添加定時(shí)任務(wù) 00 00 * * * /bin/sh ?/usr/local/nginx/sbin/cut_nginx_logs.sh //保存退出 :wq! //重啟crontab服務(wù) /etc/init.d/crond restart //查看定時(shí)任務(wù),就會(huì)看到你添加的內(nèi)容了 crontab -l
另外一個(gè)腳本格式:
2.2.1.編寫腳本
#!/bin/bash year=`date +%Y` month=`date +%m` day=`date +%d` logs_backup_path="/usr/local/nginx/logs_backup/$year$month" ? ? ? ? ? ? ? #日志存儲(chǔ)路徑 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)
2.2.2做定時(shí)任務(wù)
crontab –e 59 23 * * * bash /usr/local/nginx/shell/cut_ngnix_log.sh?? #每天23:59分開始執(zhí)行;
方法3:通過cronolog工具實(shí)現(xiàn)
3.1 下載安裝cronolog
3.1.1 下載:官網(wǎng)下載或我的百度云盤
3.1.2 安裝
1.解壓縮 # tar zxvf cronolog-1.6.2.tar.gz 2.進(jìn)入安裝文件所在目錄 # cd cronolog-1.6.2 3.運(yùn)行安裝 # ./configure # make # make install 4.查看cronolog安裝后所在目錄(驗(yàn)證安裝是否成功) # which cronolog 一般情況下顯示為:/usr/local/sbin/cronolog
3.2 使用cronolog
3.2.1.創(chuàng)建命名管道
mkfifo /usr/local/nginx/access_log_pipe
3.2.2 配置cronolog,日期按天
如果按小時(shí)使用access_%Y-%m-%d-%H.log;如果按分鐘使用access_%Y-%m-%d-%H-%M.log
3.2.3 修改配置/usr/local/nginx/conf/nginx.conf
... access_log ?/usr/local/nginx/access_log_pipe ?main; ... nohup cat /usr/local/nginx/access_log_pipe | /usr/local/sbin/cronolog /usr/local/nginx/logs/access-%Y-%m-%d.log &
3.2.4 重啟nginx
cd /usr/local/nginx/sbin ./nginx -s reload
3.2.5 查看效果
[root@app2 /]# cd /usr/local/nginx/logs/ [root@app2 logs]# ll total 3544 -rw-r--r-- 1 root root ? ? ? 0 Oct ?1 07:20 8099.access.log -rw-r--r-- 1 root root 3599534 Oct ?1 07:58 access-2016-10-01.log -rw-r--r-- 1 root root ? ? 235 Oct ?1 07:20 error.log -rw-r--r-- 1 root root ? ? ? 5 Oct ?1 06:34 nginx.pid
3.3 定期刪除日志
3.3.1 新建sh,刪除5天前的
[root@app2 sh]# pwd /usr/local/nginx/sh [root@app2 sh]# vi delete_nginx_logs.sh
添加內(nèi)容
#set the path to nginx log files log_files_path="/usr/local/nginx/logs/" save_days=5 #delete ? days ago nginx log files find $log_files_path -mtime +$save_days -exec rm -rf {} \;
3.3.2 添加定時(shí)任務(wù)
[root@localhost sh]# crontab -e 00 00 * * * /bin/sh ?/usr/local/nginx/sh/delete_nginx_logs.sh
3.4Tomcat按天切割catalina.out
安裝cronolog后,修改tomcat/bin/catalina.sh中一下內(nèi)容,注意找到準(zhǔn)確信息,2處。
原內(nèi)容:
org.apache.catalina.startup.Bootstrap "$@" start \ ?>> "$CATALINA_OUT" 2>&1 "&" 修改為:注意換行后頂格,否則啟動(dòng)報(bào)錯(cuò)。 ? ?? org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \ | /usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &
修改后按之前方式啟動(dòng)./startup.sh即可看到logs目錄下生成當(dāng)前日期文件
catalina.2018-10-11.out
-rw-rw-r-- 1 root root 0 Oct 11 15:16 catalina.out -rw-rw-r-- 1 root root 0 Oct 11 15:16 manager.2018-10-11.log -rw-rw-r-- 1 root root 0 Oct 11 15:16 host-manager.2018-10-11.log -rw-rw-r-- 1 root root 584 Oct 11 15:16 localhost.2018-10-11.log -rw-rw-r-- 1 root root 1646 Oct 11 15:16 catalina.2018-10-11.log -rw-rw-r-- 1 root root 29143 Oct 11 15:16 catalina.2018-10-11.out
總結(jié)
到此這篇關(guān)于nginx日志切割/分割之按天生成及定期刪除日志的文章就介紹到這了,更多相關(guān)nginx按天生成定期刪除日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解實(shí)現(xiàn)Nginx+Tomcat實(shí)現(xiàn)單IP、多域名、多站點(diǎn)的訪問
這篇文章主要介紹了詳解實(shí)現(xiàn)Nginx+Tomcat實(shí)現(xiàn)單IP、多域名、多站點(diǎn)的訪問的相關(guān)資料,這里提供實(shí)例幫助到大家實(shí)現(xiàn)改功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08Nginx出現(xiàn)請(qǐng)求重復(fù)提交的處理方法
在網(wǎng)絡(luò)世界的大舞臺(tái)上,Nginx 就像是一位兢兢業(yè)業(yè)的交通警察,指揮著網(wǎng)絡(luò)請(qǐng)求的有序流動(dòng),然而,有時(shí)候也會(huì)出現(xiàn)一些讓人頭疼的狀況,比如請(qǐng)求的重復(fù)提交,該如何應(yīng)對(duì)呢?本文介紹了Nginx出現(xiàn)請(qǐng)求重復(fù)提交的處理方法,需要的朋友可以參考下2024-07-07Laravel的Nginx重寫規(guī)則實(shí)例代碼
這篇文章主要介紹了Laravel的Nginx重寫規(guī)則實(shí)例代碼,需要的朋友可以參考下2017-09-09Nginx配置四層、七層網(wǎng)絡(luò)代理轉(zhuǎn)發(fā)的方法示例
nginx作為透明代理可以充分利用其高性能和靈活性來實(shí)現(xiàn)網(wǎng)絡(luò)流量的轉(zhuǎn)發(fā)和處理,本文主要介紹了Nginx配置四層、七層網(wǎng)絡(luò)代理轉(zhuǎn)發(fā)的方法示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03