(1)日誌格式定義css
[root@wjh1 ~]# vi /usr/local/nginx/conf/nginx.conf #打開nginx主配置文件#nginx
找到vim
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'瀏覽器
'$host "$request_uri" $status'bash
'"$http_referer" "$http_user_agent"';.net
combined_realip #日誌格式名可自定義# '$remote_addr #訪問地址# $http_x_forwarded_for #訪問的代理IP#debug
[$time_local]'#訪問時間# '$host#域名# "$request_uri" #請求的uri# $status#狀態碼# $http_referer #referer#代理
$http_user_agent #瀏覽器標識#日誌
#定義訪問日誌路徑及記錄日誌的格式code
access_log /tmp/access.log wjh;
(2)錯誤日誌 error_log 日誌級別
error_log 級別分爲 debug, info, notice, warn,error, crit 默認爲 crit, 該級別在日誌名後邊定義格式以下:
error_log /your/path/error.log crit; #指定路徑+日誌級別#
crit 記錄的日誌最少,而 debug 記錄的日誌最多,調成warn基本上就能夠,調成 error 級別時,錯誤日誌記錄的內容會更加豐富。
(3)某些類型的文件不記錄日誌
[root@wjh2 ~]# vi /usr/local/nginx/conf/vhosts/discuz.conf #打開虛擬主機配置文件#
#訪問日誌不記錄某些類型的文件 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ #匹配這些格式# { access_log off; #訪問日誌不記錄# } #訪問日誌不記錄某些目錄下類型的文件 location ~ static #匹配static目錄# { access_log off; } location ~ (static|cache) #匹配static和cache目錄# { access_log off; }
(2)日誌切割腳本
root@wjh-01 ~]# vim /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
## 假設nginx的日誌存放路徑爲/tmp/
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`