ngx_http_log_module 模塊經過指定的格式把請求寫入日誌.
請求登錄到location處理結束的環境中.若是重定向發生在請求處理過程當中,這或許與location原理不一樣.nginx
Example Configuration緩存
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';this
access_log /spool/logs/nginx-access.log compression buffer=32k;atom
Directives日誌
Syntax: access_log path [format [buffer=size [flush=time]] [if=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];
access_log syslog:server=address[,parameter=value] [format [if=condition]];
access_log off;
Default:
access_log logs/access.log combined;
Context: http, server, location, if in location, limit_exceptorm
設置path,format,和配置寫入日誌的緩存,幾個日誌能夠指定相同的級別.日誌記錄在syslog能夠經過指令前綴"syslog:"在第一個參數,特殊值off 取消全部access_log指令在當前的級別.
若是format沒有指定,那麼預先定義的"combined"格式被使用.
若是buffer或gzip之一參數被使用,將緩衝寫入日誌。
buffer size 必須不能超過一個atomic write 磁盤文件中, FreeBSD這個size無限制的.
若是buffering 開啓,數據將被寫入到文件中:
若是下一行日誌沒有正確的進入buffer.
若是緩存數據時間比指定的flush參數更舊
當一個worker process 從新打開日誌文件,或者關閉.
若是gzip參數被使用,那麼緩存數據在寫入文件前將被壓縮.壓縮級別能夠被設置爲1(快速,低壓縮)到9(慢速,高壓縮).默認,buffer大寫是64kB,壓縮級別爲1,當數據被壓縮原子塊,日誌文件能夠被解壓,或者使用zcat在任什麼時候間查看.
Example:
access_log /path/to/log.gz combined gzip flush=5
使用gzip去壓縮,nginx必須編譯zlib庫server
文件路徑能夠包含變量,可是這樣的日誌有一些約束:
worker processes 所使用的憑證用戶必須擁有日誌文件目錄的權限去建立文件.
緩衝寫不工做
日誌寫入的文件被打開和關閉.然而,由於經常使用文件的描述符能夠被存儲在緩存中.指定了open_log_file_cache指令valid參數,在指定的時間寫入到老的文件中.
當請求的root directory檢測存在的就寫入日誌,不存在不寫入日誌.
所以一個更好的主意就是將root和access_log寫入在相同的級別
server {
root /spool/vhost/data/$host;
access_log /spool/vhost/logs/$host;
...
if參數能夠啓用條件日誌記錄.請求將不會被記入日誌,若是 condition 爲 0或者空值.下邊的例子,這個請求將返回2xx和3xx的錯誤代碼,不會被記入log
map $status $loggable {
~^[23] 0;
default 1;
}ip
access_log /path/to/access.log combined if=$loggable;rem
Syntax: log_format name string ...;
Default:
log_format combined "...";
Context: httprequests
log format
指定log的格式.
log fomat 能夠包含普通的變量,而且這些變量只有在寫log的時候存在.
$bytes_sent
the number of bytes sent to a client
$connection
connection serial number
$connection_requests
the current number of requests made through a connection (1.1.18)
$msec
time in seconds with a milliseconds resolution at the time of the log write
$pipe
「p」 if request was pipelined, 「.」 otherwise
$time_iso8601
local time in the ISO 8601 standard format
$time_local
local time in the Common Log Format
發給客戶端的首部行擁有一個前綴"sent_http_",例如,$sent_http_content_range.
配置老是包括預約義的"combined"格式:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Default:
open_log_file_cache off;
Context: http, server, location
定義了緩存存儲常常使用日誌的文件描述符,這些名字包含變量.指令擁有下邊這些參數
max
設置cache最大的數.若是cache將要滿的時候 描述符將關閉
inactive
sets the time after which the cached descriptor is closed if there were no access during this time; by default, 10 seconds
min_uses
sets the minimum number of file uses during the time defined by the inactive parameter to let the descriptor stay open in a cache; by default, 1
valid
sets the time after which it should be checked that the file still exists with the same name; by default, 60 seconds
off
disables caching
Usage example:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;