apachenginxvarnishvary

How to add vary custom header


My application is changing the content based on user country code.

Server request flow

https request -> nginx handle the ssl termination -> varnish -> if not cacheed, get contents from apache

http rquest -> varnish -> redirect to https link -> nginx handle the ssl termination -> varnish -> if not cached get contents from apache

Using varnish GeoIP module I found the user country code and set value to the header req.http.X-Country-Code I read this header value in the application.

Problem: It always caches the first hit and serves to all the locations

I hope adding vary header will resolve my problem, could you please guide me?

I'm not sure where to add vary header nginx or apache.


Solution

  • Adding the Vary header should fix the issue and if you have access to backed code it's preferred over changing the hash in Varnish.

    Where to add it though? The best place would be to do it in the application. The application decides what to serve and it should have the power to decide what to cache and when, after all you could replace nginx or Varnish with something else in the future and there may be other caches between you and the client.