30.5 Buffering Access Logs

Problem

You need to buffer logs to reduce the opportunity of blocks to the

NGINX worker process when the system is under load.

问题

当系统处于负载状态时,启用日志缓冲区以降低 NGINX worker 进程阻塞。


Solution

Set the buffer size and flush time of your access logs:

http {
    access_log /var/log/nginx/access.log main buffer=32k flush=1m;
}

The buffer parameter of the access_log directive denotes the size

of a memory buffer that can be filled with log data before being

written to disk. The flush parameter of the access_log directive

sets the longest amount of time a log can remain in a buffer before

being written to disk.

解决方案

设置 access_log 的 buffer 和 flush 参数:

http {
    access_log /var/log/nginx/access.log main buffer=32k flush=1m;
}

buffer 参数用于设置,写入文件前的缓冲区内存大小;flush 参数设置缓冲

区内日志在缓冲区内存中保存的最长时间。


Discussion

Buffering log data into memory may be a small step toward optimi‐

zation. However, for heavily requested sites and applications, this

can make a meaningful adjustment to the usage of the disk and

CPU. When using the buffer parameter to the access_log direc‐

tive, logs will be written out to disk if the next log entry does not fit

into the buffer. If using the flush parameter in conjunction with the

buffer parameter, logs will be written to disk when the data in the

buffer is older than the time specified. When buffering logs in this

way, when tailing the log, you may see delays up to the amount of

time specified by the flush parameter.

结论

将日志数据缓冲到内存中可能是很小的一个优化手段。但是,对于有大量请求的

站点和应用程序的磁盘读写和 CPU 使用性能有重大意义。buffer 参数的功能是

当缓冲区已经写满时,日志会被写入文件中;flush 参数的功能是,当缓存中的

日志超过最大缓存时间,也会被写入到文件中,不过也有不足的地方即写入到日

志文件的日志有些许延迟。

results matching ""

    No results matching ""