Nginx 錯誤日志配置


1、Nginx錯誤日志信息介紹:

  error_log的語法格式及參數說明:

  error_log      file         level;

  關鍵字    日志文件   錯誤日志級別

  其中,關鍵字error_log 不能改變,日志文件可以指定任意存放日志的目錄,錯誤日志級別常見的有{debug,info,notice,wam,error,crit,alert,emerg},級別越高記錄的信息越少,場景一般是wam,error,crit這三個級別之一,注意不要配置info等低級別,會帶來巨大的磁盤I/O消耗。

error_log的默認值:

#default: error_log logs/error.log error;

可以放置的標簽段位:

#context: main,http,server,location

編輯主配置文件nginx.conf 增加訪問日志配置:

[root@lamp01 conf]# vim nginx.conf
worker_processes  1;
error_log logs/error.log error;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

#nginx vhosts config
    include extra/www.conf;
    include extra/bbs.conf;
    include extra/status.conf;
}

刷新配置:

/application/nginx/sbin/nginx –t

/application/nginx/sbin/nginx –s reload

查看錯誤日志文件:

cat ../logs/error.log

[root@lamp01 conf]# cat ../logs/error.log 
2019/01/08 21:01:39 [notice] 1453#0: signal process started
2019/01/08 21:20:42 [emerg] 1492#0: bind() to 0.0.0.0:80 failed (98: Address already in us
e)2019/01/08 21:20:42 [emerg] 1492#0: bind() to 0.0.0.0:80 failed (98: Address already in us
e)

 

2、Nginx訪問日志介紹:

  nginx軟件會把每個用戶訪問網站的日志信息記錄到指定的日志文件里,供網站提供分析用戶瀏覽行為等,此功能由ngx_http_log_module模塊來負責。

log_format   用來定義記錄日志的格式(可以定義多種日志格式,取不同名字即可)

access_log  用來指定日志文件的路徑及使用的何種日志格式記錄日志。

nginx日志格式默認的參數配置如下:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

日志變量說明:

status    http狀態碼,記錄請求返回的狀態,例如200、404、301等

request    用戶的http請求起始行信息

time_local   記錄訪問時間與時區

http_referer  記錄此次請求是從哪個鏈接訪問過來的,可以根據referer進行防盜鏈設置

remote_addr    記錄訪問網站的客戶端地址

remote_user   遠程客戶端用戶名稱

http_user_agent   記錄客戶端訪問信息,例如:瀏覽器,手機客戶端 

body_bytes_sent   服務器發送給客戶端的響應body字節數

http_x_forwarded_for  當前端有代理服務器時,設置web節點記錄客戶端地址配置,此參數生效的                                     前提是代理服務器上也要進行相關的x_forwarded_for設置

記錄日志access_log參數說明:

語法:

access_log path [format [buffer=size [flush = time]] [if=condition]]    

access_log path format gzip [=level] [buffer = size] [flush = time] [if = condition];    

access_log syslog:server=address[,parameter=value] [format [if=condition]];

buffer=size 為存放訪問日志的緩沖區大小,flush=time 為將緩沖區的日志刷到磁盤的時間,gzip[=level]表示壓縮級別,[if = condition]:其他條件,一般的場景,這些參數都無需配置,極端優化才可能考慮這些參數。

access_log off   off,表示不記錄訪問日志。

默認配置:access_log logs/access.log combined;

放置位置:http,server,location,if in location,limit_except

nginx記錄日志的默認參數配置如下:

access_log logs/access.log main;

編輯主配置文件nginx.conf 增加錯誤日志配置:

[root@lamp01 conf]# vim nginx.conf
worker_processes  1;
error_log logs/error.log error;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
#nginx vhosts config
    include extra/www.conf;
    include extra/bbs.conf;
    include extra/status.conf;
    access_log logs/access_www.log main;
}

刷新配置查看效果:

[root@lamp01 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@lamp01 conf]# /application/nginx/sbin/nginx -s reload
[root@lamp01 conf]# curl www.jyw1.com
www.jyw1.com
[root@lamp01 conf]# curl bbs.jyw2.com
bbs.jyw2.com
[root@lamp01 conf]# cat ../logs/access_www.log 
192.168.43.118 - - [08/Jan/2019:23:11:57 +0800] "GET / HTTP/1.1" 200 13 "-" "curl/7.19.7 (
x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"192.168.43.17 - - [08/Jan/2019:23:12:25 +0800] "GET / HTTP/1.1" 200 13 "-" "Mozilla/5.0 (W
indows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0" "-"

在高並發場景下加上buffer和flush,可以提高網站的性能:

access_log logs/access_www.log main gzip buffer=32k flush=5s;

3、nginx訪問日志輪詢切割介紹:

  默認情況nginx會把所有的訪問日志生成一個指定的訪問日志文件access.log里,時間長了會導致日志個頭很大,不利於分析日志和處理,因此,有必要對nginx按天或按小時進行切割成不同的文件保留,這里使用按天切割方法:

思路解析:

將正在寫入的Nginx日志文件(access_www.log)重命名為帶當天日期的格式文件(20190109_access_www.log),然后重新加載Nginx,生成新的Nginx日志(access_www.log)

具體切割腳本如下:

[root@lamp01 nginx]# cat /server/scripts/cut_nginx_log.sh
#!/bin/sh
Dateformat=`date +%Y%m%d -d -1day`         //定義時間
Basedir="/application/nginx"          //nginx安裝目錄
Nginxlogdir="$Basedir/logs"          //存放日志的目錄
Logname="access_www"            //日志的名稱
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1 //判斷如果有存放目錄就cd進去,否則退出
[ -f ${Logname}.log ]||exit 1   //判斷如果不存在日志名稱就執行下面命令,否則退出
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log  //把當前日志名重命名為時間日志名
$Basedir/sbin/nginx -s reload  //從新生成一個不帶時間的日志文件

效果如下:

[root@lamp01 scripts]# vim cut_nginx_log.sh 
[root@lamp01 scripts]# sh cut_nginx_log.sh 
[root@lamp01 scripts]# ll /application/nginx/logs/
總用量 32
-rw-r--r--. 1 root  root     0 1月   9 00:00 20190109_access_www.log
-rw-r--r--. 1 root  root 15974 1月   7 22:59 access.log
-rw-r--r--. 1 root  root     0 1月   9 00:15 access_www.log
-rw-r--r--. 1 nginx root 11327 1月   9 00:15 error.log
-rw-r--r--. 1 root  root     5 1月   9 2019 nginx.pid
[root@lamp01 scripts]# date -s 2019-01-10
2019年 01月 10日 星期四 00:00:00 CST
[root@lamp01 scripts]# sh cut_nginx_log.sh 
[root@lamp01 scripts]# ll /application/nginx/logs/
總用量 32
-rw-r--r--. 1 root  root     0 1月   9 00:00 20190109_access_www.log
-rw-r--r--. 1 root  root     0 1月  10 00:00 20190110_access_www.log
-rw-r--r--. 1 root  root 15974 1月   7 22:59 access.log
-rw-r--r--. 1 root  root     0 1月  10 00:00 access_www.log
-rw-r--r--. 1 nginx root 11447 1月  10 00:00 error.log
-rw-r--r--. 1 root  root     5 1月   9 21:35 nginx.pid
[root@lamp01 scripts]# 

基於多個站點:修改多個站點配置文件

[root@lamp01 extra]# vim www.conf
[root@lamp01 extra]# vim bbs.conf
[root@lamp01 extra]# cat www.conf bbs.conf
    server {
        listen       80;
        server_name  www.jyw1.com jyw1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
       access_log logs/access_www.log main;    
}
server {
        listen       80;
        server_name  bbs.jyw2.com jyw2.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
       access_log logs/access_bbs.log main;    
}

切割腳本修改如下:

[root@lamp01 scripts]# vim cut_nginx_log.sh 

#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ]||exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
/bin/mv access_bbs.log ${Dateformat}_access_bbs.log
$Basedir/sbin/nginx -s reload

效果如下:

[root@lamp01 scripts]# date -s 2019-01-12
2019年 01月 12日 星期六 00:00:00 CST
[root@lamp01 scripts]# sh cut_nginx_log.sh 
[root@lamp01 scripts]# ll /application/nginx/logs/
總用量 32
-rw-r--r--. 1 root  root     0 1月   9 00:00 20190109_access_www.log
-rw-r--r--. 1 root  root     0 1月  10 00:00 20190110_access_www.log
-rw-r--r--. 1 root  root     0 1月  12 00:00 20190112_access_bbs.log
-rw-r--r--. 1 root  root     0 1月  12 00:00 20190112_access_www.log
-rw-r--r--. 1 root  root     0 1月  12 00:01 access_bbs.log
-rw-r--r--. 1 root  root 15974 1月   7 22:59 access.log
-rw-r--r--. 1 root  root     0 1月  12 00:01 access_www.log
-rw-r--r--. 1 nginx root 11627 1月  12 00:01 error.log
-rw-r--r--. 1 root  root     5 1月   9 21:35 nginx.pid
[root@lamp01 scripts]# 

同時我們可以加入到定時任務里去:

            


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM