一、nginx訪問日志介紹
nginx軟件會把每個用戶訪問網站的日志信息記錄到指定的日志文件里,供網站提供者分析用戶的瀏覽行為等,此功能由ngx_http_log_module模塊負責,對應的官方地址為:http://nginx.org/en/docs/http/ngx_http_log_module.html.
二、訪問日志參數
nginx的訪問日志主要有以下2個參數控制
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"';
nginx記錄日志的默認參數配置如下:
access_log logs/access.log main;
三、訪問日志配置說明
日志格式的定義說明
語法如下:
定義語法 log_format name string ...;
其配置位置在http標簽內。
日志格式說明如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
其中,log_format為日志格式關鍵參數,不能變。
main是為日志格式指定的標簽,記錄日志時通過這個main標簽選擇指定的格式。其后所接的所有內容都是可以記錄的日志信息,所有的日志段以空格分割,一行可以記錄多個。不同列的意義如下:
$remote_addr 記錄訪問網站的客戶端地址; $http_x_forwarded_for 當前端有代理服務器時,設置web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器上也進行了相關的x_forwarded_for設置; $remote_user 遠程客戶端用戶名稱; $time_local 記錄訪問時間與時區; $request 用戶的http請求起始行信息; $status http狀態碼,記錄請求返回的狀態,例如200、404、301等; $body_bytes_sent 服務器發給客戶端的響應body字節數; $http_referer 記錄此次請求是從哪個鏈接訪問呢過來的,可以根據referer進行防盜鏈設置; $http_user_agent 記錄客戶端訪問信息,例如:瀏覽器、手機客戶端等;
在沒有特殊要求的情況下,采用默認的配置即可,更多可以設置的記錄日志信息的變量見:http://nginx.org/en/docs/http/ngx_http_log_module.html
記錄日志的access_log參數說明
下面是有關access_log參數的官方說明:
語法如下:
access_log path [format [buffer=size] [gzip[=level]] [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.conf,配置日志的格式如下:
[root@nginx conf]# sed -n '21,23 s/#//gp' nginx.conf.default log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
把上述內容放到nginx.conf 的 http 標簽的首部,如下:
[root@nginx conf]# cat nginx.conf worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; keepalive_timeout 65; include extra/dmtest1.conf; include extra/dmtest2.conf; include extra/dmtest3.conf; }
然后在每個虛擬主機里進行配置,使其使用上述格式記錄用戶訪問日志。以 www.dmtest1.com 站點為例,命令如下:
[root@nginx conf]# cat extra/dmtest1.conf server { listen 80; server_name www.dmtest1.com dmtest1.com; location / { root html/dmtest1; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } access_log logs/access_dmtest1.log main; }
如果不指定日志格式就會采用默認的combined格式記錄日志。
檢查語法重新加載配置文件:
[root@nginx conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@nginx conf]# systemctl reload nginx
用瀏覽器模擬用戶訪問生成日志,在服務器上查看日志結果,如下:
[root@nginx conf]# curl www.dmtest1.com www.dmtest1.com [root@nginx conf]# ls -l ../logs/access_dmtest1.log -rw-r--r-- 1 root root 95 8月 27 06:24 ../logs/access_dmtest1.log [root@nginx conf]# tail -1 ../logs/access_dmtest1.log 192.168.200.102 - - [27/Aug/2018:06:24:36 +0800] "GET / HTTP/1.1" 200 16 "-" "curl/7.29.0" "-"
使用谷歌瀏覽器訪問的日志結果如下:
[root@nginx conf]# tail -10 ../logs/access_dmtest1.log 192.168.200.1 - - [27/Aug/2018:06:36:58 +0800] "GET / HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
日志格式和日志內容對應說明如下:
$remote_addr 對應的是真實日志里的192.168.200.1,即客戶端的IP。 $remote_user 對應的是第二個中杠"-",沒有遠程用戶,所以用"-"填充。 [$time_local 對應的是[27/Aug/2018:06:36:58 +0800]。 "$request" 對應的是"GET/HTTP/1.1"。 $tatus 對應的是200狀態碼,200表示正常訪問。 $body_bytes_sent 對應的是16字節,即響應body的大小。 "$http_referer" 對應的是"-",由於是直接打開域名瀏覽的,因此,referer沒有值。 $http_user_agent 對應的是"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "$http_x_forwarded_for" 對應的是"-",因為web服務沒有使用代理,因此此處為"-"
下面針對日志配置進行深入說明。
可以在記錄日志參數中加上buffer和flush選項,這樣可以在高並發場景下提升網站訪問性能。加該選項的命令如下:
access_log path [format [buffer=size] [gzip[=level]] [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]]; access_log off;
具體配置如下:
[root@nginx conf]# cat extra/dmtest1.conf server { listen 80; server_name www.dmtest1.com dmtest1.com; location / { root html/dmtest1; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #access_log logs/access_dmtest1.log main; access_log logs/access_dmtest1.log main gzip buffer=32k flush=5s; #access_log off; }
五、nginx訪問日志輪訓切割
默認情況下nginx會把所有的訪問日志生成到一個指定的訪問日志文件access_log里,但這樣一來,時間長了就會導致日志文件很大,不利於日志的分析和處理,因此,有必要對nginx日志按天或小時進行切割,使其分成不同的文件保存。這里使用按天切割的方法。
具體切割腳本如下:
[root@nginx shell]# cat cut_nginx_log.sh #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-27 07:19:30 #Name:cut_nginx_log.sh #Description: Dateformat=`date +%Y%m%d` Basedir="/application/nginx" Nginxlogdir="$Basedir/logs" Logname="access_dmtest1" [ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1 [ -f ${Logname}.log ] || exit 1 /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log #$Basedir/sbin/nginx -s reload systemctl reload nginx
腳本實現切割nginx的思想為將正在寫入的nginx日志(access_dmtest1.log)改名為帶日期的格式文件(20180827_access_dmtest1.log),然后平滑重新加載nginx,生成新的nginx日志(access_dmtest1.log)。
把腳本加入計划任務:
[root@nginx shell]# cat >>/var/spool/cron/root << EOF > #cut ngixn access log by dm 2018-8-27 > 00 00 * * * /bin/sh /server/scripts/shell/cut_nginx_log.sh >/dev/null 2&1 > EOF [root@nginx shell]# crontab -l #time sync by dm at 2018-8-20 */5 * * * * /usr/sbin/ntpdate -u ntp.api.bz >/dev/null 2>$1 #cut ngixn access log by dm 2018-8-27 00 00 * * * /bin/sh /server/scripts/shell/cut_nginx_log.sh >/dev/null 2&1
最終日志切割效果如下:
[root@nginx logs]# ll 總用量 32 -rw-r--r-- 1 root root 0 8月 27 07:35 20180827_access_dmtest1.log -rw-r--r-- 1 root root 0 8月 27 07:35 access_dmtest1.log -rw-r--r-- 1 root root 14076 8月 27 04:41 access.log -rw-r--r-- 1 root root 10098 8月 27 06:36 error.log -rw-r--r-- 1 root root 5 8月 26 21:56 nginx.pid