Nginx log

ngx_http_log_module 模块通过指定的格式把请求写入日志.
请求登陆到location处理结束的环境中.如果重定向发生在请求处理过程中,这或许与location原理不同.

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 [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_except

设置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库

文件路径可以包含变量,但是这样的日志有一些约束:
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;
}

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


Syntax: log_format name string ...;
Default:
log_format combined "...";
Context: http

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;

原文地址:https://www.cnblogs.com/idnf/p/4630187.html