High performing web server using Nginx

Nginx is web server you should consider as an alternative to Apache. Why? Because it doesn’t need so many resources, it’s faster and can handle huge traffic much better. Benchmarks vary between small to severalfold improvements. WordPress.com uses it to serve 70,000 req/sec and over 15 Gbit/sec of traffic. Curious enough?

There are three reasons to use Nginx

  • You have a cheap low-performance server which needs to catch a second wind
  • You have high-performance server and you want to handle more traffic
  • Although you have enough performance, being more efficient saves trees 🙂

I don’t think Nginx is holy grail. Compared to Apache it doesn’t have so much functionality and community is incomparably smaller. However, what does, does great and for the most people it’s enough.  And, it’s not a newbie, according to the April 2014 Web Server Survey, is now used by approximately 37.7% of the websites.

Nginx for personal low-budget solutions
If you have just a basic server, usually for personal website, e-shop or similar, you probably experienced exceeding memory limit. Unlike a storage, memory is a really expensive frequent bottleneck of low-cost solutions. Nginx consumes much less memory than Apache. In fact, the difference is so dramatic it’s almost a bit unbelievable. For example check Dreamhost case study.

Another example can be running heavy web app such as Magento. They recommend to set up memory limit to crazy 512MB which can be all you have for whole server. Apache, in contrast with Nginx, just can’t handle such conditions.

Nginx for high-traffic website
Nginx has been designed for high traffic and literary to address c10K problem –  concurrently handling ten thousand connections. Unlike traditional servers, Nginx doesn’t rely on threads to handle requests. Instead, it uses a much more scalable event-driven (asynchronous) architecture.

WordPress.com is one of Nginx users. This quote from 2008 speaks for itself.

Nginx was only software we tested which could handle 8000 (live traffic, not benchmark) requests/second on a single server.
We are regularly serving about 8-9k requests/second  and about 1.2Gbit/sec through a few Nginx instances and have plenty of room to grow!

And nowadays they serve even ten-times more. I really recommend to read these two articles talking about their experience. Here and here.

Nginx as a self-hosted CDN solution

Incredible speed of serving static assets such as images, css, javascript, etc.(up to 10x faster) in combination with its efficiency makes Nginx ideal candidate for CDN solution. A browser has limits on the number of connections it can make to a specific domain. If your website has a lot of static assets, having even basic self-hosted CDN can speed it up by parallelising downloads.

If you are interested in this topic, have a look at this case study by Htpc Beginner of building self-hosted CDN for WordPress.

Btw, if you think about CDN seriously, you should consider using G-WAN which supposed to be even faster for static content, consuming similar amount of memory and better optimised for multi-core CPUs.

Nginx as a reverse proxy cache
If you really need to use Apache, you can use Nginx as a proxy cache before processing to main web server. Catching all traffic on early beginning it will bypass Apache if cache for request is available. In this case, you don’t need to use Varnish or similar tool for caching and you get even more flexibility for future proxy tweaking. You can also define rules for serving static content by Nginx and dynamic files by Apache.

Disadvantages
Whether we like it or not, we’re living in Apache world. If you want to use some open source web app such as WordPress, Magento, etc. on Nginx, you can face smaller or bigger compatibility issues. From my experience, most of them caused by absence of .htaccess file (mod_rewrite, authentication, etc.). Also in fact, most websites really don’t get so many concurrent hits as to gain large performance/memory benefits from Nginx.

Summary
It’s about what works best for you! You can always tweak an Apache a bit by turning off KeepAlive, setting up MaxClients and installing Varnish. Or you can really cut the edge and play with G-WAN, which I guess is the most efficient web sever today… if you are geeky and skilled programmer enough.