3

I have a production Rails application deployed on Unicorn with nginx in front for static file serving. I now need some features of Varnish and I'm wondering how to introduce it.

Some people put Varnish in front while others put nginx in front. I haven't heard of anyone using just Varnish and Unicorn, but I imagine it's valid.

Those with nginx in front seem to be doing so for gzip and SSL termination. SSL is not relevant to my application, but gzip is. Varnish is getting it soon I think, so I could live without for a little while in the interest of simplicity.

Are there any other pros/cons to either approach? Is there a canonical best practice layout for this?

wormcontrol
  • 31
  • 1
  • 2

1 Answers1

10

Heroku (for example) puts nginx in front of varnish probably for the reasons you mentioned.

So, nginx -> varnish -> app servers.

At my work, we use Varnish in front of everything (varnish -> nginx -> apache/wsgi) and it works pretty well. The thinking there is that if there's a cache hit, it is served from the highest point in the stack. There are a few quirks though. VCL is very specific to its task and as such makes it a little difficult to do things like redirects (say, for mobile detection) that nginx could handle pretty easily. In general, it's just a little less flexible since that's not quite what it was meant to do.

Generally, from what I've read (and based on my experience at work) it's probably better to put nginx in front of the stack with varnish in between it and your app servers.

The one downside to this approach when using Unicorn is that you won't be able to use the unix socket method with nginx and unicorn since you'll have to hit varnish in between and varnish doesn't support them. That's probably a minor thing though.

UPDATE: For some additional, more concrete reasons to choose one over the other, check out this answer's comments. It depends on if you need SSL, ESI and gzip compression.

mirthlab
  • 221
  • 2
  • 6