How can I optimize nginx? From benchmarking it seems Apache2 is faster for static delivery

Solution 1:

In order to get realistic results you must have realistic tests. It's completely feasible apache is faster for your test scenario, but are you really serving just a single one-kilobyte file?

As you're using mpm-prefork, it's safe to say nginx will consume significantly less memory when there are several concurrent transfers. Concurrent transfers pile up easily if you have large files or your clients have slow internet connections. Nginx will win hands down when you have enough concurrent transfers for Apache to eat up all your memory.

One can argue this is not really an issue as long as there's enough memory for Apache. However, this is not the whole truth. When less memory is consumed by http server, more content from file system will be cached, and every disk seek eliminated will be a small performance victory.

Solution 2:

On a connection that isn't bandwidth-constrained (as I suspect your tiny little connections here are), gzip-compressed content will be slower to transfer than non-gzip-compressed content, because of the extra CPU involved. Compressing your content is usually faster because smaller chunks of data transfer faster, but with that little test it probably won't help. Try comparing apples with apples and see what you get.

Solution 3:

Have you tried optimizing the performance by serving the files from a ramdisk? Some VPSes are notoriously bad for IOwait time, caused by contended access to the disk.

Try running dstat on the server while the ab process is running, see whether the disks are taking a massive hit.