22

I see a lot of talk about using nginx with Varnish and I do not understand why. Isn't nginx enough?

Nginx has SSI, has reverse proxy cache, is lightweight, has SSL, can work with cgi, fpm, etc.

Varnish has the same things, but no ssl, and no cgi support.

3
  • "Better" is really subjective. It depends on your use cases...
    – voretaq7
    Dec 21, 2011 at 17:30
  • 7
    Sometimes a simple question do the job better than complex ones. I am very concerned about actions like that from the people who closed this question assuming it was no good to the "original Q&A format". I am searching about this subject and I was very interested to see the outcome of this debate.
    – Roger
    Apr 29, 2012 at 7:29
  • varnish has powerful vcl configuration, purge, reverse proxy cache, ESI nginx can work with FCGI, reverse proxy cache, ssi, no purge(maybe there is a module) performance wise they are almost the same for static files, caching...if you have any question i can help Aug 27, 2012 at 11:04

2 Answers 2

14

I've found varnish ~5% faster for small static files than nginx -- varnish in front of apache, or varnish in front of a web app server would be big gains; but in front of nginx, the benefit is pretty negligible (especially with overheads and extra complexity taken into account)

1
  • Can you provide some test results to support this? What if I'm already using nginx+opcache. Is there any benefit of using varnish then?
    – Serious
    Oct 16, 2014 at 12:28
23

First, to clarify: Nginx is a web server, with all of the features and complexity that entails. It also has caching capability, but that is not its primary design goal.

Varnish is not a web server. It cannot fill that role (not without a truly evil VCL, anyway). Its role is to cache content provided by another server. If needed, it can alter the request or the response.

If nginx can handle your traffic, then it is enough. If nginx is not able to keep up, then one way to increase its capacity is by placing a cache in front of it and having the cache handle as many of the requests as possible.

As an example, we use Apache to run a number of relatively complex PHP websites from a cluster of web servers. When we started to experience capacity problems, we placed a pair of Varnish servers in front of the Apache cluster. The Varnish hosts now handle 85% of all incoming requests without bothering the Apache backend.

4
  • So, Varnish caches the dynamic content generated by PHP? Or just the other stuff?
    – Alix Axel
    Dec 1, 2012 at 22:14
  • 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.
    – Insyte
    Dec 6, 2012 at 22:52
  • I was asking about your specific example. It baffles me how it's possible to cache dynamic content without messing up the dynamic application (PHP sessions for starters). Caching static content seems a bit redundant to me.
    – Alix Axel
    Dec 7, 2012 at 1:10
  • 4
    It depends on the dynamic content. Say, for example, that the page is a blog post with comments. It may be extremely expensive to generate the comment thread but perfectly acceptable to cache it for 5 minutes. On the other hand, caching the dynamic content returned by a webmail application would clearly be problematic. You can even create a mixed case: Cache the full output of an ecommerce app, but tell varnish to call back to the backend to populate the shopping cart indicator.
    – Insyte
    Jan 2, 2013 at 23:34

Not the answer you're looking for? Browse other questions tagged .