1、錯誤日志配置 錯誤日志屬于核心功能模塊的參數
worker_processes 1; error_log /data/logs/nginx/error.log error; #一般配置這一行即可 events {worker_connections 1024; }
語法規則:error_log file level
錯誤的日志級別有[debug|info|notice|warn|error|crit|alert|emerg],級別越高,記錄的信息越少,生產場景一般是warn|error|crit這三個級別之一
可以放置的標簽段為:main,http,server,location
2、訪問日志配置
①定義日志格式(放置的http標簽內),在沒有特殊要求的情況下,采用默認配置即可
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
②在每個虛擬主機配置應用
root@c1 ~]# cat /application/nginx/conf/vhost/www.heboan.com.conf server {listen 80;server_name www.heboan.com;location / {root /data/www/heboan;index index.html index.htm;}access_log /data/logs/nginx/www.heboan.com.log; #配置這一行即可 }#沒有什么需求,建議在生產環境關閉訪問日志 access_log off
3、訪問日志的輪詢切割
①創建輪詢切割腳本
#vim /data/shell/nginx_cut_log.sh#!/bin/bash # LOGDIR=/data/logs/nginx LOGBKDIR=$LOGDIR/`date +%Y-%m` NGINX_SBIN=/application/nginx/sbin/nginxlogrotate () {local Ifor I in `ls $LOGDIR`;doif [ -f $LOGDIR/$I ];thenlog_bkname=$LOGBKDIR/${I}_`date +%d`.gzlog_file=$LOGDIR/$Icat $log_file|gzip >$log_bknamerm -f $Ifidone${NGINX_SBIN} -s reload }delempdir () {local Ifor I in $*;doIFEMPTY=`ls $I`[ "$IFEMPTY" == "" ] && rmdir $Idone }#do the log rotating [ ! -d $LOGBKDIR ] && mkdir -p $LOGBKDIR logrotate#delete the outdated bakcup log files find $LOGDIR -name "*log*" -mtime +7 -exec rm -rf {} \;#delete the empty directory under $LOGDIR ALLBAKLOCATION=`find $LOGDIR -type d` delempdir $ALLBAKLOCATION
②通過定時任務每天00.01點準時執行/data/shell/nginx_cut_log.sh
[root@c1 ~]# crontab -l #cut nginx access log by heboan 01 00 * * * /usr/bin/bash /data/shell/nginx_cut_log.sh
4、不記錄不需要的訪問日志
在實際工作中,對負載均衡器健康節點檢查或某些特定文件(比如圖片、js、css)的日志,一般不需要記錄下來,因為在統計pv時是按照頁面計算的,而日志寫入太頻繁會消耗大量的I/0,降低服務的性能
具體配置方法如下:
location ~ .*\.(js|png|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {access_log off; }
5、訪問日志權限設置
加入日志目錄為/app/logs,則授權方法如下:
chown -R root.root /app/logs chmod -R 700 /app/logs
不需要在日志目錄上給nginx用戶讀或寫許可,但很多網友都沒注意這個問題,他們把該權限直接給了nginx或apache用戶,這就成為了安全隱患。
?