Nginx日志格式配置介紹
by:授客 QQ:1033553122
測試環境
CentOS 6.5-x86_64
nginx-1.10.0
配置例子
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $request_time $upstream_response_time $request_length $bytes_sent $body_bytes_sent $gzip_ratio $connection_requests "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/nginx-access.log main;
說明:如下,有時候我們可以把日志配置json串格式,方便其它程序使用
log_format main '{
"remote_addr":"$remote_addr",
"remote_user":"$remote_user",
"time_local":"$time_local",
"request":"$request",
"status":"$status",
"request_time":"$request_time",
"upstream_response_time":"$upstream_response_time",
"request_length":"$request_length",
"bytes_sent":"$bytes_sent",
"body_bytes_sent":"$body_bytes_sent",
"gzip_ratio":"$gzip_ratio",
"connection_requests":"$connection_requests",
"http_referer":"$http_referer",
"http_user_agent":"$http_user_agent",
"http_x_forwarded_for":"$http_x_forwarded_for"
}';
配置說明
可在相同層級(個人理解:這里的層級為下文的Context范圍)指定多個日志
語法說明:
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off; # 關閉當前層級上的指定日志,即不記錄日志
Default:access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
注意:路徑path必須存在,如果開啟了gzip日志壓縮,則不能通過控制台實時查看日志了。
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http
format變量說明
$remote_addr
發起請求的客戶端所在ip地址
$remote_user
發起請求的客戶端用戶名稱,獲取不到則顯示為 -
$time_local
用來記錄訪問時間與時區(依賴nginx服務器本地時間),形如 20/Aug/2017:21:15:19 +0800,獲取不到則顯示為 -
$time_iso8601
類似$time_local,不同的是這里采用ISO 8601標准格式
$request
記錄發起的請求,形如
POST /zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw HTTP/1.1
$status
記錄響應狀態,比如 200
$request_time
記錄請求處理時間(以秒為單位,攜帶毫秒的解決方案),從讀取客戶端第一個字節開始算起,到發送最后一個字節給客戶端的時間間隔(原文:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client)
$upstream_response_time
記錄nginx從后端服務器(upstream server)獲取響應的時間(以秒為單位,攜帶毫秒的解決方案),多個請求的時間以逗號分隔
參考鏈接:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_response_time
$request_length
記錄請求長度(包括請求行,請求頭,請求體)
$gzip_ratio
記錄nginx gzip壓縮比例,獲取不到則顯示為 -
$bytes_sent
發送給客戶端的字節數
$body_bytes_sent
發送給客戶端的響應體字節數
$connection_requests
單個連接的並發請求數(the current number of requests made through a connection (1.1.18)
$http_referer
記錄請求引用頁面地址
$http_user_agent
記錄用戶代理信息(通常是瀏覽器信息
$http_x_forwarded_for
當為了承受更大的負載使用反向代理時,web服務器不能獲取真實的客戶端IP,$remote_addr獲取到的是反向代理服務器的ip,這種情況下,代理服務器通常會增加一個叫做x_forwarded_for的信息頭,把連接它的真實客戶端IP加到這個信息頭里,這樣就能保證網站的web服務器能獲取到真實IP,獲取不到則顯示為 -
$connection
連接序列號
$msec
寫入日志的時間(以秒為單位,攜帶毫秒的解決方案)(原文:time in seconds with a milliseconds resolution at the time of the log write)
$pipe
如果為管道請求則顯示為p,否則顯示為 .
日志輸出樣例
# tail -f /usr/local/ngnix/logs/access.log
192.168.1.101 - - [20/Aug/2017:22:28:44 +0800] "POST /zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw HTTP/1.1" 200 0.365 0.365 764 794 302 - 1 "http://192.168.1.102/zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0" "-"
# tail -f /usr/local/ngnix/logs/access.log
{
"remote_addr":"192.168.1.101",
"remote_user":"-",
"time_local":"31/Aug/2017:10:37:01 +0800",
"request":"GET /zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw HTTP/1.1",
"status":"200",
"request_time":"0.562",
"upstream_response_time":"0.562",
"request_length":"546",
"bytes_sent":"8013",
"body_bytes_sent":"7520",
"gzip_ratio":"-",
"connection_requests":"1",
"http_referer":"-",
"http_user_agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0",
"http_x_forwarded_for":"-"
}
參考鏈接:
http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log