nginx日志导致IO负载较高,如何处理

使用Nginx来收集日志,访问量较大,在将收集的数据直接记录磁盘文件时,导致磁盘IO过高,机器直接挂掉。

为减少磁盘IO操作,将日志写如内存分区;但日志量太大,很容易将内存写满。

Nginx支持日志压缩功能,具体如下:

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

If either the buffer or gzip (1.3.10, 1.2.7) parameter is used, writes to log will be buffered.

 The buffer size must not exceed the size of an atomic write to a disk file. For FreeBSD this size is unlimited

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.

If the gzip parameter is used, then the buffered data will be compressed before writing to the file. The compression level can be set between 1 (fastest, less compression) and 9 (slowest, best compression). By default, the buffer size is equal to 64K bytes, and the compression level is set to 1. Since the data is compressed in atomic blocks, the log file can be decompressed or read by “zcat” at any time.

Example:

access_log /path/to/log.gz combined gzip flush=5m;
For gzip compression to work, nginx must be built with the zlib library.

nginx支持压缩后写日志,使用此功能可以将日志文件放在内存分区,减少磁盘IO操作。然后定时对日志轮询,将日志写入磁盘或传输到日志处理集群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值