日志對付統計排錯來說很是有利的。本文總結了nginx日志相關的設置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
nginx有一個很是機動的日志記錄模式。每個級此外設置可以有各自獨立的會見日志。日志名目通過log_format呼吁來界說。ngx_http_log_module是用來界說請求日志名目標。
語法: access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off;
默認值: access_log logs/access.log combined;
設置段: http, server, location, if in location, limit_except
gzip壓縮品級。
buffer配置內存緩存區巨細。
flush生存在緩存區中的最長時間。
不記錄日志:access_log off;
利用默認combined名目記錄日志:access_log logs/access.log 或 access_log
logs/access.log combined;
語法: log_format name string …;
默認值: log_format combined “…”;
設置段: http
name暗示名目名稱,string暗示等義的名目。log_format有一個默認的無需配置的combined日志名目,相當于apache的combined日志名目,如下所示:
log_format combined '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
假如nginx位于負載平衡器,新加坡電信服務器 馬來西亞服務器,squid,nginx反向署理之后,web處事器無法直接獲取到客戶端真實的IP地點了。 $remote_addr獲取反向署理的IP地點。反向署理處事器在轉發請求的http頭信息中,可以增加X-Forwarded-For信息,用來記錄 客戶端IP地點和客戶端請求的處事器地點。PS: 獲取用戶真實IP 拜見http://www.ttlsa.com/html/2235.html如下所示:
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
日志名目答允包括的變量注釋如下:
$remote_addr, $http_x_forwarded_for 記錄客戶端IP地點
$remote_user 記錄客戶端用戶名稱
$request 記錄請求的URL和HTTP協議
$status 記錄請求狀態
$body_bytes_sent 發送給客戶端的字節數,不包羅響應頭的巨細; 該變量與Apache模塊mod_log_config里的“%B”參數兼容。
$bytes_sent 發送給客戶端的總字節數。
$connection 毗連的序列號。
$connection_requests 當前通過一個毗連得到的請求數量。
$msec 日志寫入時間。單元為秒,精度是毫秒。
$pipe 假如請求是通過HTTP流水線(pipelined)發送,pipe值為“p”,不然為“.”。
$http_referer 記錄從哪個頁面鏈接會見過來的
$http_user_agent 記錄客戶端欣賞器相關信息
$request_length 請求的長度(包羅請求行,請求頭和請求正文)。
$request_time 請求處理懲罰時間,單元為秒,精度毫秒; 從讀入客戶端的第一個字節開始,直到把最后一個字符發送給客戶端后舉辦日志寫入為止。
$time_iso8601 ISO8601尺度名目下的當地時間。
$time_local 通用日志名目下的當地時間。
[warning]發送給客戶端的響應頭擁有“sent_http_”前綴。 好比$sent_http_content_range。[/warning]
實譬喻下:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$gzip_ratio" $request_time $bytes_sent $request_length';
log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';
open_log_file_cache max=1000 inactive=60s;
server {
server_name ~^(www.)?(.+)$;
access_log logs/$2-access.log main;
error_log logs/$2-error.log;
location /srcache {
access_log logs/access-srcache.log srcache_log;
}
}
}
3. open_log_file_cache指令