Nginx access_log 默认刷新时间

Nginx access_log default flush time

默认情况下,nginx 将其缓冲区刷新到 access_log 的频率如何?

手册中没有信息,只有设置语法:

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

我自己对此做了一些研究,据我所知,如果不指定 flush 参数,nginx 根本不会刷新缓冲区。 (或者,如果是,则超过 20 分钟。)

唯一的例外是如果您重新启动服务器,日志会在重新启动前刷新。

因此,如果您指定了缓冲区,您也应该确保指定刷新时间。

来自nginx文档[1]:

When buffering is enabled, the data will be written to the file:

-  if the next log line does not fit into the buffer;
-  if the buffered data is older than specified by the flush parameter (1.3.10, 1.2.7);
-  when a worker process is re-opening log files or is shutting down.

当然如果不指定flush参数第二个条件不能成立

[1] http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

除非您指定 flush 选项(即使您指定了 buffer 选项),否则 Nginx 不会刷新。

这里有一个例子,说明如何每五分钟将 8k 的数据包缓冲到日志中:

access_log  /var/log/nginx/access.log  main buffer=8k flush=5m;