最後更新: 2019-05-09
--with-file-aio
When both AIO and sendfile are enabled on Linux,
AIO is used for files that are larger than or equal to the size specified in the directio directive,
while sendfile is used for files of smaller sizes or when directio is disabled.
--with-threads
files can be read and sent using multi-threading (1.7.11), without blocking a worker process:
Read and send file operations are offloaded to threads of the specified pool.
If the pool name is omitted, the pool with the name “default” is used.
aio
Syntax: aio on | off | threads[=pool];
Default: aio off;
Context: http, server, location
Enables or disables the use of asynchronous file I/O
* On Linux necessary to enable directio (Default: Off)
* only be used for reading blocks that are aligned on 512-byte boundaries (or 4K for XFS).
File’s unaligned end is read in blocking mode.
threads
Context: 'http', 'server', 'location'
read and sent using multi-threadin g (1.7.11), without blocking a worker process.
By default, multi-threading is disabled, it should be enabled with the --with-threads configuration parameter.
location /video/ { sendfile on; aio threads; }
進階
configuration of thread pools
# in the 'main' context
thread_pool MyThreadPool threads=32 max_queue=65536;
# in the 'http', 'server', or 'location' context
aio threads=MyThreadPool;
If the task queue is overloaded, NGINX rejects the request and logs this error:
thread pool "NAME" queue overflow: N tasks waiting
If the max_queue parameter isn’t specified, the value 65536 is used by default.
zero => the thread pool will only be able to handle as many tasks as there are threads configured;
Syntax: directio size | off;
Default: directio off;
Enables the use of the O_DIRECT flag, when reading files that are larger than or equal to the specified size.
It can be useful for serving large files
i.e.
directio 4m;
# Sets the alignment for directio.
# Default: 512-byte
# using XFS under Linux, it needs to be increased to 4K
directio_alignment 512;
Syntax: sendfile on | off;
Default: sendfile off;
sendfile() is called with the SF_NODISKIO flag which causes it not to block on disk I/O, but,
instead, report back that the data are not in memory.
nginx then initiates an asynchronous data load by reading one byte.
aio can be used to pre-load data for sendfile()
aio and sendfile
When both AIO and sendfile are enabled on Linux:
file size >= directio aio
file size < directio sendfile
while sendfile is used for files of smaller sizes or when directio is disabled. location /video/ { sendfile on; aio on; directio 8m; directio_alignment 4k; }