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 complains about slow and interrupted downloads which is quite annoying.

After digging the Nginx docs, I 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.

Merry Christmas!

Hi! It’s almost Christmas, and it’s time to say Merry Christmas to everyone out here. This year, I really want to thank so many people for their help and support. Although some of my friends are going elsewhere to study and my mother’s having some illness, with the help of different people, I’m feeling much better now.

Finally, I recorded a nice song (“Joy to the World”) for this merciful moment, enjoy! And, Merry Christmas to everyone and have a nice new year!

[Music Sharing] How To Train Your Dragon Soundtrack

My blog is mainly about programming but I want to tell you: I <3 music also! This time, I want to share you a nice soundtrack called “How To Train Your Dragon”. Let’s enjoy!

What is a router explained

(This article is written for my classmates.)

As everyone knows, the internet is extremely big and it seamlessly connects every people, in the world. But there’s one question: how can the internet knows where to pass the data around? The answer to this problem is the router. The router, by definition, is a device that forwards data between computer networks. To understand what a router actually does, we need to have a basic understanding to the structures of the internet. Continue reading What is a router explained