在 Nginx 中,什么场景适合开启 sendfile,什么场景适合开启 directio


在 Nginx 中,什么场景适合开启 sendfile,什么场景适合开启 directio

php nginx lnmp

qwedw 8 years, 9 months ago

directio是啥...

壮汉HSI answered 8 years, 9 months ago

http://nginx.org/en/docs/http/ngx_http_core_module.html#aio


 location /video/ {
    sendfile on;
    sendfile_max_chunk 256k; 
    aio threads;
    directio 512k;
    output_buffers 1 128k;
}

启用aio时会自动启用directio,小于directio定义的大小的文件则采用sendfile进行发送,超过或等于directio定义的大小的文件,将采用aio线程池进行发送,也就是说aio和directio适合大文件下载.因为大文件不适合进入操作系统的buffers/cache,这样会浪费内存,而且Linux AIO(异步磁盘IO)也要求使用directio的形式.

sendfile_max_chunk可以减少阻塞调用sendfile()所花费的最长时间.因为Nginx不会尝试一次将整个文件发送出去,而是每次发送大小为256KB的块数据.

注意,Nginx从1.7.11开始为AIO引入了线程池支持,能够使用多线程读取和发送文件,以免工人进程被阻塞.要启用多线程支持,configure时需要显式加入--with-threads选项.

shahim answered 8 years, 9 months ago

Your Answer