Caching with Varnish & Varying over custom-set HTTP headers

1.4k Views Asked by At

I'm developing your standard high traffic ecommerce website and want to setup caching with Varnish. The particular thing on this setup is that the application will return different content depending on the user's particular location.

So my plans are these:

  • Setup Nginx with GeoIP module, so I can get a X-Country: XX header on all the requests going to the app backends.
  • Configure the Rails application to always return a "Vary: X-Country" response header.
  • Put the Varnish server behind the Nginx and the app backends, so it can cache multiple versions of the objects served by Rails, and serve them based on the request headers set by Nginx (not the client browser)

Does anyone have experience with a setup like this? Anything I should be aware of?

1

There are 1 best solutions below

0
On

If GeoIP lookup is slow, and/or you want to enable people to override the country setting, you could use a country cookie and have the front-end Varnish check for it.

  • If there is no country cookie, forward the request to your nginx back-end for GeoIP lookup. Nginx serves a redirect with a Set-Cookie: country=us header. If you want to avoid redirects and support cookie-refusing clients/robots, ngingx can forward it to Rails and still try to set the country cookie in the response. Or Varnish can capture the redirect response and do a "restart" with the newly set cookie and go to the back-end
  • If you have already have a country cookie, use this in your Varnish hash

If Rails can do GeoIP resolving, you don't need Ngingx, except when you use it to serve files...