Nginx中文文档
DocsHome
  • 贡献导引
  • 介绍
    • 安装 nginx
    • 从源码构建 nginx
    • 初学者指南
    • 管理指南
    • 控制 nginx
    • 连接处理方式
    • 设置哈希
    • 调试日志
    • 记录日志到 syslog
    • 配置文件度量单位
    • 命令行参数
    • Windows 下的 nginx
    • QUIC 和 HTTP/3 支持
    • nginx 如何处理请求
    • 服务器名称
    • 使用 nginx 作为 HTTP 负载均衡器
    • 配置 HTTPS 服务器
    • nginx 如何处理 TCP/UDP 会话
    • 关于 nginScript
  • 其他
    • Linux 软件包
  • How-To
    • 在 Win32 平台上使用 Visual C 构建 nginx
    • 设置 Amazon EC2 的 Nginx Plus 环境
    • 使用 DTrace pid 提供程序调试 nginx
    • 转换重写规则
    • WebSocket 代理
  • 开发
    • 贡献指南
    • 开发指南
  • 模块参考
  • 核心功能
    • HTTP
      • ngx_http_core_module
      • ngx_http_access_module
      • ngx_http_addition_module
      • ngx_http_api_module
      • ngx_http_auth_basic_module
      • ngx_http_auth_jwt_module
      • ngx_http_auth_request_module
      • ngx_http_autoindex_module
      • ngx_http_browser_module
      • ngx_http_charset_module
      • ngx_http_dav_module
      • ngx_http_empty_gif_module
      • ngx_http_f4f_module
      • ngx_http_fastcgi_module
      • ngx_http_flv_module
      • ngx_http_geo_module
      • ngx_http_geoip_module
      • ngx_http_grpc_module
      • ngx_http_gunzip_module
      • ngx_http_gzip_module
      • ngx_http_gzip_static_module
      • ngx_http_headers_module
      • ngx_http_hls_module
      • ngx_http_image_filter_module
      • ngx_http_index_module
      • ngx_http_js_module
      • ngx_http_keyval_module
      • ngx_http_limit_conn_module
      • ngx_http_limit_req_module
      • ngx_http_log_module
      • ngx_http_map_module
      • ngx_http_memcached_module
      • ngx_http_mirror_module
      • ngx_http_mp4_module
      • ngx_http_perl_module
      • ngx_http_proxy_module
      • ngx_http_random_index_module
      • ngx_http_realip_module
      • ngx_http_referer_module
      • ngx_http_rewrite_module
      • ngx_http_scgi_module
      • ngx_http_secure_link_module
      • ngx_http_session_log_module
      • ngx_http_slice_module
      • ngx_http_spdy_module(过时)
      • ngx_http_split_clients_module
      • ngx_http_ssi_module
      • ngx_http_ssl_module
      • ngx_http_status_module(过时)
      • ngx_http_stub_status_module
      • ngx_http_sub_module
      • ngx_http_upstream_module
      • ngx_http_upstream_conf_module
      • ngx_http_upstream_hc_module
      • ngx_http_userid_module
      • ngx_http_uwsgi_module
      • ngx_http_v2_module
      • ngx_http_xslt_module
    • Mail
      • ngx_mail_core_module
      • ngx_mail_auth_http_module
      • ngx_mail_proxy_module
      • ngx_mail_ssl_module
      • ngx_mail_imap_module
      • ngx_mail_pop3_module
      • ngx_mail_smtp_module
    • Stream
      • ngx_stream_core_module
      • ngx_stream_access_module
      • ngx_stream_geo_module
      • ngx_stream_geoip_module
      • ngx_stream_js_module
      • ngx_stream_keyval_module
      • ngx_stream_limit_conn_module
      • ngx_stream_log_module
      • ngx_stream_map_module
      • ngx_stream_proxy_module
      • ngx_stream_realip_module
      • ngx_stream_return_module
      • ngx_stream_split_clients_module
      • ngx_stream_ssl_module
      • ngx_stream_ssl_preread_module
      • ngx_stream_upstream_module
      • ngx_stream_upstream_hc_module
      • ngx_stream_zone_sync_module
    • 其他
      • ngx_google_perftools_module
由 GitBook 提供支持
在本页
  • 示例配置
  • 指令
  • access_log
  • log_format
  • open_log_file_cache
  • 原文档
  1. 核心功能
  2. Stream

ngx_stream_log_module

上一页ngx_stream_limit_conn_module下一页ngx_stream_map_module

最后更新于5年前

ngx_stream_log_module 模块(1.11.4)按指定的格式写入会话日志。

示例配置

log_format basic '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time';

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

指令

access_log

-

说明

语法

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

默认

access_log off;

上下文

stream、server

如果使用 buffer 或 gzip 参数,则将缓冲 log 的写入。

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

启用缓冲后,以下情况数据将写入文件:

  • 如果下一个日志行不能放入缓冲区

  • 如果缓冲的数据早于 flush 参数指定的数据

  • 当 worker 进程重新打开日志文件或正在关闭时

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

示例:

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

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

文件路径可以包含变量,但是这样的日志有一些约束:

  • 缓冲写入将不起作用

if 参数启用条件日志。如果 condition 计值为 0 或空字符串,则不会记录会话。

log_format

-

说明

语法

log_format name [escape=default\|json\|none] string ...;

默认

——

上下文

stream

指定日志格式,例如:

log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

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

open_log_file_cache

-

说明

语法

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off;

默认

open_log_file_cache off;

上下文

stream、server

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

  • max

    设置缓存中的最大描述符数,如果缓存变满,则最近最少使用(LRU)的描述符将被关闭

  • inactive

    如果在此期间没有发生访问,则设置关闭缓存描述符的时间。默认为 10 秒

  • min_uses

    设置在 inactive 参数定义的时间内文件使用的最小数量,使描述符在缓存中保持打开状态。默认为 1

  • valid

    设置检查同名文件是否仍然存在的时间。默认为 60 秒

  • off

    禁用缓存

用法示例:

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

原文档

设置缓冲日志写入的路径、和配置。可以在同一级别指定多个日志。可通过在第一个参数中指定 syslog: 前缀来配置记录到 。特殊值 off 取消当前级别的所有 access_log 指令。

被 worker 进程使用凭据的应具有在具有此类日志的目录中创建文件的权限

每个日志写入都会打开和关闭文件。但是,由于频繁使用的文件的描述符可以存储在中,因此可以在 指令的 valid 参数指定的时间内继续写入旧文件

http://nginx.org/en/docs/stream/ngx_stream_log_module.html
示例配置
指令
access_log
log_format
open_log_file_cache
syslog
格式
缓存
open_log_file_cache
用户