新聞中心
Nginx日志對(duì)于統(tǒng)計(jì)、系統(tǒng)服務(wù)排錯(cuò)很有用,**Nginx日志分為多種,主要分為兩種:**access_log(訪問日志)和error_log(錯(cuò)誤日志)。通過訪問日志我們可以得到用戶的IP地址、瀏覽器的信息,請(qǐng)求的處理時(shí)間等信息。錯(cuò)誤日志記錄了訪問出錯(cuò)的信息,可以幫助我們定位錯(cuò)誤的原因,下面為大家詳細(xì)講解一下Nginx日志配置具體方法。

nginx日志配置
nginx有一個(gè)非常靈活的日志記錄模式。每個(gè)級(jí)別的配置可以有各自獨(dú)立的訪問日志。日志格式通過log_format命令來定義。ngx_http_log_module是用來定義請(qǐng)求日志格式的。
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;
#默認(rèn)值: access_log logs/access.log combined;
#配置段: http, server, location, if in location, limit_except
#gzip壓縮等級(jí)。
#buffer設(shè)置內(nèi)存緩存區(qū)大小。
#flush保存在緩存區(qū)中的最長時(shí)間。
#不記錄日志:access_log off;
#使用默認(rèn)combined格式記錄日志:
access_log logs/access.log
或
access_log logs/access.log combined;
log_format指令
語法: log_format name string …;
默認(rèn)值: log_format combined “…”;
配置段: http
name表示格式名稱,string表示等義的格式。log_format有一個(gè)默認(rèn)的無需設(shè)置的combined日志格式,相當(dāng)于apache的combined日志格式,如下所示:
log_format combined '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
如果nginx位于負(fù)載均衡器,squid,nginx反向代理之后,web服務(wù)器無法直接獲取到客戶端真實(shí)的IP地址了。 $remote_addr獲取反向代理的IP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請(qǐng)求的http頭信息中,可以增加X-Forwarded-For信息,用來記錄 客戶端IP地址和客戶端請(qǐng)求的服務(wù)器地址。
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 記錄請(qǐng)求的URL和HTTP協(xié)議
$status 記錄請(qǐng)求狀態(tài)
$body_bytes_sent 發(fā)送給客戶端的字節(jié)數(shù),不包括響應(yīng)頭的大??; 該變量與Apache模塊mod_log_config里的“%B”參數(shù)兼容。
$bytes_sent 發(fā)送給客戶端的總字節(jié)數(shù)。
$connection 連接的序列號(hào)。
$connection_requests 當(dāng)前通過一個(gè)連接獲得的請(qǐng)求數(shù)量。
$msec 日志寫入時(shí)間。單位為秒,精度是毫秒。
$pipe 如果請(qǐng)求是通過HTTP流水線(pipelined)發(fā)送,pipe值為“p”,否則為“.”。
$http_referer 記錄從哪個(gè)頁面鏈接訪問過來的
$http_user_agent 記錄客戶端瀏覽器相關(guān)信息
$request_length 請(qǐng)求的長度(包括請(qǐng)求行,請(qǐng)求頭和請(qǐng)求正文)。
$request_time 請(qǐng)求處理時(shí)間,單位為秒,精度毫秒; 從讀入客戶端的第一個(gè)字節(jié)開始,直到把最后一個(gè)字符發(fā)送給客戶端后進(jìn)行日志寫入為止。
$time_iso8601 ISO8601標(biāo)準(zhǔn)格式下的本地時(shí)間。
$time_local 通用日志格式下的本地時(shí)間。
發(fā)送給客戶端的響應(yīng)頭擁有“sent_http_”前綴。 比如$sent_http_content_range.
實(shí)例如下:
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;
}
}
}
open_log_file_cache指令
語法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
默認(rèn)值: open_log_file_cache off;
配置段: http, server, location
對(duì)于每一條日志記錄,都將是先打開文件,再寫入日志,然后關(guān)閉??梢允褂胦pen_log_file_cache來設(shè)置日志文件緩存(默認(rèn)是off),格式如下:
參數(shù)注釋如下:
max:設(shè)置緩存中的最大文件描述符數(shù)量,如果緩存被占滿,采用LRU算法將描述符關(guān)閉。
inactive:設(shè)置存活時(shí)間,默認(rèn)是10s
min_uses:設(shè)置在inactive時(shí)間段內(nèi),日志文件最少使用多少次后,該日志文件描述符記入緩存中,默認(rèn)是1次
valid:設(shè)置檢查頻率,默認(rèn)60s
off:禁用緩存
實(shí)例如下:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
log_not_found指令
語法: log_not_found on | off;默認(rèn)值: log_not_found on;配置段: http, server, location
是否在error_log中記錄不存在的錯(cuò)誤。默認(rèn)是。
log_subrequest指令
語法: log_subrequest on | off;
默認(rèn)值: log_subrequest off;
配置段: http, server, location
是否在access_log中記錄子請(qǐng)求的訪問日志。默認(rèn)不記錄。
rewrite_log指令
說明:由ngx_http_rewrite_module模塊提供的。用來記錄重寫日志的。對(duì)于調(diào)試重寫規(guī)則建議開啟。
語法: rewrite_log on | off;
默認(rèn)值: rewrite_log off;
配置段: http, server, location, if
啟用時(shí)將在error log中記錄notice級(jí)別的重寫日志。
error_log指令
語法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默認(rèn)值: error_log logs/error.log error;
配置段: main, http, server, location
配置錯(cuò)誤日志。
分享名稱:詳解Nginx日志配置具體方法
當(dāng)前鏈接:http://www.5511xx.com/article/dhcdccd.html


咨詢
建站咨詢
