Varnish + Nginx in comparison with Nginx alone, which is better?

Like others, I confused with the combine of Varnish + Nginx and Nginx alone on my server. But since I optimized new server with only Nginx, FastCGI Cache, Memcache. I’ve found a lot of talk about using Nginx with Varnish or not around the Internet. Isn’t Nginx enough?

The Quick Answer: I choosed Nginx alone without Varnish because I’ve found Varnish ~5% faster for small static files than Nginx but I will have to spend more time to maintaining and configuring it to works as I experct. It is not worth my effort!
This arctile will explain the reason that I don’t use Varnish with Nginx like Apache before.

What’s Varnish cache

Varnish is a web application accelerator. You install it in front of your web application and it will speed it up significantly.
Varnish stores data in virtual memory and leaves the task of deciding what is stored in memory and what gets paged out to disk to the operating system. This helps avoid the situation where the operating system starts caching data while they are moved to disk by the application.

Why I won’t use Varnish with Nginx!

Varnish is memory and cpu eater

Varnish is heavily threaded, with each client connection being handled by a separate worker thread. When the configured limit on the number of active worker threads is reached, incoming connections are placed in an overflow queue; only when this queue reaches its configured limit will incoming connections be rejected. For a lowest VPS plan on Digital Ocean with just 1 CPU, 512MB RAM, and hight traffic website; Varnish will become the most usage of server resources, the next is MySQL / MariaDB service.
[digitalocean]

Varnish lacks support for SSL and SPDY

Here is the answer of Why no SSL question from Varnish. This means that if I want to use Varnish, but have the need for SSL, I need a SSL proxy in front. I’ve tried a couple of commonly recommended TLS/SSL terminators, but I’ve yet to succeed in getting either to work on my server setup. Why I need to spent more time to learn another TLS/SSL terminators or patch Varnish to get it works (I can’t ^^).
A requirement of SDPY is SSL, Varnish is not support SPDY too.

Static files caching

The main difference between nginx and varnish when caching are flexibility and purging. Varnish is more flexible when you have complex cache structure and/or. Purging/Banning is another key advantage of varnish over Nginx.
Nginx can handle static files itself pretty fast. It has built-in support for fastcgi_cache but it doesn’t have mechanism to purge cached content built-in. With fastcgi_cache_purge module which adds ability to purge content from FastCGI, proxy, SCGI and uWSGI caches. I don’t want to install new packages (include addition dependencies) if the Nginx has same functions with 3rd plugins, it’ll working better than Varnish.
If you running a large or very high traffic website, the CDN is best solution to replace static files caching like Nginx or Varnish.

Conclusion

Varnish is more advanced in terms of caching because Varnish caches whatever you tell it to cache. It can cache just the PHP output, just the static files, both, or neither. It’s a very powerful tool. But Nginx is more suitable as a web server.
On a low-end VPS with very basic hardware requirement to build a perfect web server. I prefer use Nginx without Varnish with FastCGI cache, Memcache to get better perfomance and effort. The perfect and over optimize server is not best server ^^

17 thoughts on “Varnish + Nginx in comparison with Nginx alone, which is better?”

  1. Hi Mr Quân, I just read this article and http://www.narga.net/recommended-nginx-configuration-high-traffic-wordpress/.
    I and waiting your article on Varnish cache as you say.
    And in this time, I learn how to using Nginx, with fcgi_cache, memcached , opcache, in Ubuntu 14.04, Nginx 1.6. Php 5.4, Mysql 5.5.
    It so fast for me. https://developers.google.com/speed/pagespeed/insights/?url=rongchoidalat.com&tab=mobile

    But I want to aske about, if I using Varnish, how to config work with current config of Nginx.

    Thank.

  2. I was thinking of buying cloud based hosting. Which one would you suggest for best possible performance for a website which has really high real time traffic?

  3. Hi Nguyễn,

    Interesting article. Thank you! However, you can pass https SSL requests directly to Apache or Nginx. It’s sounded like you were saying that you need both, however, Varnish front cache and apache OR Nginx OR Nginx front end and Apache backend would work. We use Varnish front cache and Apache web server at times, but you are right. And Nginx standalone or with Apache backend for easy .htaccess support is great.

    Thanks!

  4. Thank you for the extra info.

    The first thing that comes to my mind when i see both on my CWP webpanel is that both are a caching proxy, just a layer. There is not reason to have more than just 1 cache because you’ll eat the resources for the dummy 2nd cache.

    Also, because you say that Nginx is faster and less heavyload i choose it this time. (Last was using Varnish)
    See you ;D

  5. Instead of using nginx with varnish you can use nginx with fastcgi cache. fastcgi cache is inbuilt microcache system which works way better than varnish and less resource hungry. I would like to suggest one more thing. If you are doing something on nginx so do it on nginx. Varnish is multi threaded and on heavy traffic it might under perform; but yes it is good for low resource servers.

  6. A little late to the party but I did my own tests and using varnish + nginx as a reverse proxy shaved a second off my load times from 2.3 seconds to 1.3 seconds a page versus just using nginx without Varnish.

Comments are closed.