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