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 ^^

nginx, web hosting

17 Comments

  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?

    • I would suggest SSD VPS – such as those found at DigitalOcean, Linode and Vultr. However, these VPS hosts will not help support your VPS outside the hardware and network if that.

      If you want actual support for your SSD VPS, try SpectroHost at https://www.spectrohost.com/cloud

  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!

    • Oh and by the way, the best great Control Panel implementation with Nginx and Apache backend comes off to be VestaCP right now. Check it out!

      • I don’t know too much about VestaCP but I see it has rick features. At this time, I manage my VPSs by hands but I install ServerPilot for several customers, it’s lighweight and had most necessary features.

  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.

    • Why don’t you trying with PHP7, it’s increase double loadtime speed without varnish :)

      • So if I have already installed PHP7 + FastCGI + Memcached + Nginx I don’t need Varnish?

        • In my opinion, you won’t find the difference between varnish or not. Do you want to trying by yourself?

    • If you don’t set nginx caching or other caching, of course, varnish cache with nginx will be swifter vs. nginx with no caching.

      Try setting up nginx caching and test again. I bet you will be surprised at the results.

  7. i have planning to buy vps hosting with ngink but i dont known spec and where good vps for traffic 2-3k/day.


Add a Comment

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

Comment *
Name *
Email *
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.