非常教程

Nginx参考手册

ngx_http_log_module

ngx_http_log_module

  • 示例配置
  • 指令
  • access_log
  • log_format
  • open_log_file_cache

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

请求会在处理结束的位置的上下文中记录。如果在请求处理期间发生内部重定向,它可能与原始位置不同。

示例配置

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;

指令

句法:

access_log path [format buffer=sizegzip=level] flush=time];access_log off;

默认:

access_log logs/access.log combined;

环境:

http, server, location, if in location, limit_except

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

如果使用bufferor gzip(1.3.10,1.2.7)参数,则写入日志将被缓存。

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

当启用缓冲时,数据将被写入文件中:

  • 如果下一个日志行不适合缓冲区;
  • 如果缓冲数据比flush参数(1.3.10,1.2.7)指定的更早;
  • 当工作进程重新打开日志文件或正在关闭时。

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

示例:

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

要使gzip压缩起作用,必须使用zlib库构建nginx。

文件路径可以包含变量(0.7.6+),但是这些日志有一些限制:

  • 工作进程使用其凭据的用户应具有使用此类日志在目录中创建文件的权限;
  • 缓冲写入不起作用;
  • 该文件打开并关闭每个日志写入。但是,由于经常使用的文件的描述符可以存储在缓存中,因此可以在由open_log_file_cache指令的valid参数指定的时间内继续写入旧文件
  • 在每次写入日志期间,检查请求根目录的存在,如果不存在,则不创建日志。因此,指定root和access_log同一级别是一个好主意: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;

句法:

log_format name escape=default|json string ...;

默认:

log_format combined "...";

语境:

http

指定日志格式。

escape参数(1.11.8)允许设置jsondefault字符逸出变量,默认情况下,default逸出被使用。

日志格式可以包含公共变量和仅在日志写入时存在的变量:

$bytes_sent发送到客户端的字节数$connection连接序列号$connection_requests做出的请求的当前数目通过连接(1.1.18)$msec时间(秒)与在日志写入时的毫秒分辨率$pipep”如果请求被流水线.“ ”否则$request_length请求长度(包括请求行,标题和请求体)$request_time与毫秒分辨率秒请求处理时间; 从客户端读取第一个字节之间的时间,并在最后一个字节发送到客户端$status响应状态后的日志写入$time_iso8601以ISO 8601标准格式$time_local本地时间的公用日志格式

在现代的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_content_range。,发送到客户端的标题行具有前缀“sent_http_

该配置始终包含预定义的“ combined”格式:

log_format combined '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

句法:

open_log_file_cache max=N inactive=time valid=time;open_log_file_cache off;

默认:

open_log_file_cache off;

语境:

http, server, location

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

max设置缓存中描述符的最大数量;如果高速缓存变满,则最近最少使用(LRU)描述inactive符关闭,如果在此期间没有访问,则高速缓存描述符关闭的时间;默认情况下,10秒min_uses设置在inactive参数定义的时间内文件使用的最小数量,以使描述符在缓存中保持打开状态;默认情况下,1 valid设置应检查文件是否仍然存在同名的时间; 默认情况下,60秒off禁用缓存

用法示例:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
ngx_http_log_module
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module