Optimizing Nginx for (large) file delivery

7 February 2021

Here are some useful tips if you want to optimize your Nginx for large file delivery. Use at your own risk!

  1. Turn off sendfile. The Linux sendfile call is known to have throughput degradation when in high load. Disabling it helps to keep a higher throughput at high load. Also, when serving large files with sendfile, there are no ways to control read ahead.
  2. Enable TCP nopush. TCP nopush fills the TCP packet to its maximum size before sending. This can help increase throughput if you’re serving large files.
  3. Use Nginx’s directio to load file. Using directio can help to improve performance by skipping a bunch of steps happened in the kernel when reading files, thus speed up the throughput.
  4. Enable the use of libaio for optimal performance libaio allows asynchronous I/O to be done in kernel, which results in faster read and write speed. However, it needs libaio to be installed and re-compiling your Nginx in order to have it supported. I used the following flow to recompiling Nginx with aio support.

Please be aware that this last part is rather old. I do not advise performing the next step, unless you do it with a more recent version of Nginx. I cannot guarantee if libaio will work at all nowadays. But feel free to try this for yourself. Leave a comment when you know more.

# Install libaio on RHEL/CentOS
yum install libaio libaio-devel -y

wget http://nginx.org/download/nginx-1.9.4.zip
unzip -q nginx-1.9.4.zip
cd nginx-1.9.4

# Configure Nginx according to your needs, but it should also include
# --with-file-aio in order to use libaio
./comnfigure --with-file-aio
make

The complete nginx.conf should look like this:

http {
    ...
    sendfile off;
    tcp_nopush on;
    aio on;
    directio 512; # required if you're using Linux and uses aio
    ...
}

There are also some lower-level tweaks like mounting your disks with noatime flags and use ext4/xfs when serving files.

Many thanks to the source for this article: https://licson.net/post/optimizing-nginx-for-large-file-delivery/

Leave a Reply

Your email address will not be published. Required fields are marked *

Unrelated Content

19 June 2025
Choosing the live-stream host that suits your needs

Discover how to choose the right live-stream host for your organization. Compare popular platforms like DaCast, Wowza, Vimeo, Kick, ReStream and IBM.

READ MORE
27 November 2020
Motivation behind this website

For every service that is offered for a lot of money on the internet, there's always a free or much cheaper alternative, especially if you embrace open-source software. For live-streaming this is also very much the case. We love that,

READ MORE
23 January 2021
Introduction to the new how-to article in the Knowledge Base.

I wrote a new guide on setting up your own live-stream server. Mostly due to your requests. I took the opportunity to simplify and shorten the guide a bit. If you follow this guide step by step, and use your brain a tiny bit, you'll be fine. This guide has so far been tested on […]

READ MORE
25 January 2021
Optimizing First-Frame Bitrate for HLS

When responding to an HLS request, the streaming server determines which video quality (i.e., ts file) the client will attempt to play before switching to a lower or higher quality video. This switch depends on available bandwidth and device type. Switching to the wrong quality first degrades the user experience considerably. For instance, sending a […]

READ MORE
25 January 2021
Adaptive Bit-rate Streaming

“What is adaptive streaming?” This question often leads to answers filled with technical terms like “HLS”, “MPEG-DASH”, “segments” and “codec“. This article is written for people who need to understand the principles, but don’t need to actually implement the technology. If you are looking for specific technical information on adaptive streaming then I recommend you read up on it on this website and elsewhere.

READ MORE
2 February 2021
About Bradmax Video Player WordPress plugin

Let's talk about our favorite video player plugin for WordPress. Here are some reasons why Bradmax Player is our favorite. And below that are detailed instructions on how to set it up. Setting it up may at first seem a bit awkward, but it really isn't. First you must obviously install the Bradmax Player plugin […]

READ MORE
Copyright © 2025. 
All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram