directio

The 'directio' directive enables direct I/O for reading and writing files, bypassing the OS cache.

Syntaxdirectio size;
Defaultnone
Contexthttp, server, location
Arguments1

Description

The 'directio' directive in NGINX is used to configure direct I/O operations for file handling, particularly useful in cases where high performance is desired while serving large files. When enabled, data is read and written directly between the disk and the application, avoiding the operating system's cache. This can lead to reduced latency and increased throughput for applications that access large files frequently.

This directive comes with a single argument that specifies the size of the direct I/O buffer. The size given is aligned to a block size that the underlying filesystem uses. For example, if your block size is 4 KB, setting 'directio 4k;' would be appropriate. When accessed, this directive helps to optimize the way file data is handled, particularly in high-performance scenarios or systems where memory resources are constrained.

The 'directio' directive can be declared in the contexts of http, server, and location blocks, allowing flexibility in configuration at different levels of the NGINX hierarchy. It's important to note that while this feature can provide significant benefits, it may not be necessary for all workloads, particularly those that do not involve large file accesses regularly, as the overhead of managing direct I/O can sometimes outweigh its performance benefits.

Config Example

location /downloads {
    directio 4k;
    root /var/www/files;
}

Ensure that the filesystem supports direct I/O; otherwise, the directive may not have any effect.

Be careful about aligning the buffer size with the underlying filesystem's block size to avoid performance penalties.

Using direct I/O may bypass the kernel page cache, which could lead to lower performance for small file access patterns.

← Back to all directives