Varnish and Ajax

If you have endpoints on your site that serve up both HTML and JSON depending on the request type, not the URL, then you need to tell Varnish to add this as an extra hash so that it doesn’t return JSON to clients expecting HTML, and vice versa. Oddly, I’ve not actually seen any examples of how to this, so I came up with:

sub vcl_hash {
    if (req.http.X-Requested-With == "XMLHttpRequest") {

(Django uses X-Requested-With for is_ajax(), so it is consistent with that – if you were wondering).

Posted on August 9, 2013 at 11:03 pm by Carlos Corbacho · Permalink
In: Apache, Django, Varnish, Web

Leave a Reply

You must be logged in to post a comment.