日志對(duì)于統(tǒng)計(jì)排錯(cuò)來說非常有利的。本文總結(jié)了nginx日志相關(guān)的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
nginx有一個(gè)非常靈活的日志記錄模式。每個(gè)級(jí)別的配置可以有各自獨(dú)立的訪問日志。日志格式通過log_format命令來定義。ngx_http_log_module是用來定義請(qǐng)求日志格式的。
1. access_log指令
語(yǔ)法: access_log path [format [buffer=size [flush=time]]];
復(fù)制代碼 代碼如下:
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ū)中的最長(zhǎng)時(shí)間。
不記錄日志:access_log off;
使用默認(rèn)combined格式記錄日志:access_log logs/access.log 或 access_log logs/access.log combined;
2. log_format指令
語(yǔ)法: log_format name string …;
默認(rèn)值: log_format combined “…”;
配置段: http
name表示格式名稱,string表示等義的格式。log_format有一個(gè)默認(rèn)的無(wú)需設(shè)置的combined日志格式,相當(dāng)于apache的combined日志格式,如下所示:
復(fù)制代碼 代碼如下:
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ù)器無(wú)法直接獲取到客戶端真實(shí)的IP地址了。 $remote_addr獲取反向代理的IP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請(qǐng)求的http頭信息中,可以增加X-Forwarded-For信息,用來記錄 客戶端IP地址和客戶端請(qǐng)求的服務(wù)器地址。如下所示:
復(fù)制代碼 代碼如下:
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
日志格式允許包含的變量注釋如下:
復(fù)制代碼 代碼如下:
$remote_addr, $http_x_forwarded_for 記錄客戶端IP地址
新聞熱點(diǎn)
疑難解答
圖片精選