Module ngx_http_log_module

ngx_http_log_module模块以指定格式写入请求日志。

请求记录在处理结束位置的上下文中。如果在请求处理期间发生了internal redirect,则它可能与原始位置不同。

Example Configuration

log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log /spool/logs/nginx-access.log compression buffer=32k;

Directives

Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

access_log off;
默认值: access_log logs/access.log combined;
上下文: httpserverlocationif in locationlimit_except

设置缓冲日志写入的路径,格式和配置。可以在同一级别上指定多个日志。可以通过在第一个参数中指定“ syslog:”前缀来配置登录到syslog。特殊值off会取消当前级别上的所有access_log指令。如果未指定格式,则使用 sched 义的“ combined”格式。

如果使用buffergzip(1.3.10,1.2.7)参数,则将写入日志。

Note

缓冲区大小不得超过对磁盘文件进行原子写入的大小。对于 FreeBSD,此大小是无限的。

启用缓冲后,数据将被写入文件:

如果使用gzip参数,则缓冲的数据将在写入文件之前被压缩。压缩级别可以设置为 1(最快,更少压缩)到 9(最慢,最佳压缩)。默认情况下,缓冲区大小等于 64K 字节,并且压缩级别设置为 1.由于数据是按原子块压缩的,因此可以随时对日志文件进行解压缩或以“ zcat”读取。

Example:

access_log /path/to/log.gz combined gzip flush=5m;

Note

为了使 gzip 压缩正常工作,必须使用 zlib 库构建 nginx。

文件路径可以包含变量(0.7.6),但是此类日志具有一些约束:

server {
    root       /spool/vhost/data/$host;
    access_log /spool/vhost/logs/$host;
    ...

if参数(1.7.0)启用条件日志记录。如果condition的值为“ 0”或空字符串,则不会记录请求。在以下示例中,将不会记录响应代码为 2xx 和 3xx 的请求:

map $status $loggable {
    ~^[23]  0;
    default 1;
}

access_log /path/to/access.log combined if=$loggable;

Syntax: log_format name [escape=default|json|none] string ...;
Default: log_format combined "...";
Context: http

指定日志格式。

escape参数(1.11.8)允许在变量中设置jsondefault字符转义,默认情况下使用default转义。 none值(1.13.10)禁用转义。

对于default转义,字符“ "”,“ \”以及其他值小于 32(0.7.0)或大于 126(1.1.6)的字符将转义为“ \xXX”。如果找不到变量值,将记录连字符(“ -”)。

对于json转义,将转义 JSON strings中不允许的所有字符:字符“ "”和“ \”转义为“ \"”和“ \\”,值小于 32 的字符转义为“ \n”,“ \r” ,“ \t”,“ \b”,“ \f”或“ \u00XX”。

日志格式可以包含公用变量,以及仅在写入日志时存在的变量:

$bytes_sent

$connection

$connection_requests

$msec

$pipe

$request_length

$request_time

$status

$time_iso8601

$time_local

Note

在现代的 Nginx 版本中,变量$status(1.3.2,1.2.2),$bytes_sent(1.3.8,1.2.5),$connection(1.3.8,1.2.5),$connection_requests(1.3.8,1.2.5), $msec(1.3.9,1.2.6),$request_time(1.3.9,1.2.6),$pipe(1.3.12,1.2.7),$request_length(1.3.12,1.2.7),$time_iso8601(1.3.12, 1.2.7)和$time_local(1.3.12,1.2.7)也可以用作公共变量。

发送到客户端的标题行带有前缀“ sent_http_”,例如$sent_http_content_range

该配置始终包括 sched 义的“ 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;
默认值: open_log_file_cache off;
上下文: httpserverlocation

定义一个缓存,该缓存存储名称中包含变量的常用日志的文件 Descriptors。该指令具有以下参数:

Usage example:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
首页