sendfile
The `sendfile` directive enables or disables the use of the `sendfile()` system call for transferring files in response to client requests.
Description
The sendfile directive is part of the NGINX HTTP core module and optimizes file transmission by allowing the server to send files directly from disk to the network connection without needing to copy data between the kernel and user space. This results in improved performance and reduced CPU usage especially when serving static files like images, stylesheets, or JavaScript files.
This directive accepts a single argument, which can be either on or off. When set to on, NGINX will utilize the sendfile() function for outputting files to the client, which is especially beneficial when working with large files. Conversely, setting it to off disables the use of this functionality, relying instead on more traditional methods of sending files which may be less efficient.
The sendfile directive can be included in various contexts such as http, server, and location, as well as within if directives in location contexts. Note that while enabling sendfile generally results in better performance, it is important to ensure that your server's configuration and application logic can safely handle the use of this directive, as it may introduce complications with certain configurations, such as those involving TLS or specific logging scenarios.
Config Example
http {
sendfile on;
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}Using sendfile with non-blocking I/O can lead to unexpected behavior due to the way data is transmitted.
Certain configurations like proxying or certain modules (e.g., HTTP/2) may not interact well with sendfile, so be sure to test performance accordingly.