Optimizing Nginx for (large) file delivery

Some times ago, I have a need to host some big files for open download. At first, I think Nginx will perform pretty well without muck configuration. In reality, there are complaints about slow and interrupted downloads which is quite annoying.

I ended up using Xender for PC to transfer the files, but after digging the Nginx docs, I did find some nice changes that can fix these problems and produce a high throughput. Here’s my tweaks made to the nginx.conf file:

  1. Turn off sendfileThe 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 readahead.
  2. Enable TCP nopushTCP 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 fileUsing directio can help improving 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 performancelibaio 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.

The complete nginx.conf should look like this:

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

Published by

Licson Lee

Hi! I'm Licson. I'm a student who loves programming.

4 thoughts on “Optimizing Nginx for (large) file delivery”

    1. It is okay if you don’t agree with my adjustments, but please make some contributions (further tips/tricks, recommendations to improve) before saying you don’t like it. Also, please prevent the use of foul words. That’s not really making your comments helpful.

    2. Apparently monkeys are smarter than you or you wouldn’t have been on this blog looking for answers. Go make your own blog if you’re so smart. You’re useless.

Leave a Reply

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