nginx伺服器日志相關指令主要有兩條
1.一條是log_format,用來設定日志格式.
2. 另外一條是access_log,用來指定日志檔案的存放路徑、格式和緩存大小,一般在nginx的配置檔案中日記配置(/usr/local/nginx/conf/nginx.conf)。
nginx的log_format有很多可選的參數用于訓示伺服器的活動狀态,預設的是:
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format的詳細參數如下:
參數 | 說明 | 示例 |
$remote_addr | 用戶端位址 | 211.28.65.253 |
$remote_user | 用戶端使用者名稱 | -- |
$time_local | 通路時間和時區 | 18/Jul/2012:17:00:01 +0800 |
$request | 請求的URI和HTTP協定 | "GET /article-10000.html HTTP/1.1" |
$http_host | 請求位址,即浏覽器中你輸入的位址(IP或域名) | www.it300.com 192.168.100.100 |
$status | HTTP請求狀态 | 200 |
$upstream_status | upstream狀态 | 200 |
$body_bytes_sent | 發送給用戶端檔案内容大小 | 1547 |
$http_referer | url跳轉來源 | https://www.baidu.com/ |
$http_user_agent | 使用者終端浏覽器等資訊 | "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; |
$ssl_protocol | SSL協定版本 | TLSv1 |
$ssl_cipher | 交換資料中的算法 | RC4-SHA |
$upstream_addr | 背景upstream的位址,即真正提供服務的主機位址 | 10.10.10.100:80 |
$request_time | 整個請求的總時間 | 0.205 |
$upstream_response_time | 請求過程中,upstream響應時間 | 0.002 |
舉例配置檔案:
#vim /usr/local/nginx/conf/nginx.conf
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for '
'"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';
2. nginx日志格式
日志對于統計排錯來說非常有利的,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是用來定義請求日志格式的
2.1 access_log 指令
文法:
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;
2.2 log_format指令
文法: 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位址和用戶端請求的伺服器位址,如下
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
123.125.71.86 - - [17/Jul/2015:15:24:23 +0800] "GET
/data/cache/forum_slide.js?GKl HTTP/1.1" 200 4082
"http://www.iyunv.com/forum.php?fid=54&digest=1" "Mozilla/5.0 (compatible;
Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" -
日志格式允許包含的變量注釋如下:
$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 #通用日志格式下的本地時間
發送給用戶端的響應頭擁有"sent_http_". 比如$sent_http_content_range.
執行個體如下:
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;
}
}
}
2.3 open_log_file_cache指令
文法:
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
預設值: open_log_file_cache off;
配置段: http,server,location
對 于 每 一 條 日 志 記 錄 , 都 将 是 先 打 開 文 件 , 再 寫 入 日 志 , 然 後 關 閉 。 可 以 使 用 open_log_file_cache 來設定日志檔案緩存(預設是 off),格式如下:
參數注釋如下:
max: 設定緩存中的最大檔案描述符數量,如果緩存被占滿,采用LRU算法将描述符關閉。
inactive: 設定存活時間,預設是10s
min_uses: 設定在inactive時間段内,日志檔案最少使用多少次後,該日志檔案描述符計入緩存重,預設是1次
valid:設定檢查頻率,預設是60s
off: 禁用緩存
執行個體如下:
open_log_file_cache_max=1000 inactive=20s valid=1m min_user=2;
2.4 log_not_found指令
文法: log_not_found on | off;
預設值: log_not_found on;
配置段: http, server, location 是否在 error_log 中記錄不存在的錯誤。預設是。
2.5 log_subrequest指令
文法: log_subrequest on | off;
預設值: log_subrequest off;
配置段: http, server, location 是否在 access_log 中記錄子請求的通路日志。預設不記錄。
2.6 rewrite_log 指令
由 ngx_http_rewrite_module 子產品提供的。用來記錄重寫日志的。對于調試重寫規則建議 開啟。 Nginx 重寫規則指南
文法: rewrite_log on | off;
預設值: rewrite_log off;
配置段: http, server, location, if 啟用時将在 error log 中記錄 notice 級别的重寫日志。
2.7 error_log 指令
文法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
預設值: error_log logs/error.log error;
配置段: main, http, server, location 配置錯誤日志。