Search code examples
cachingjsonpvarnishvarnish-vcl

handle jsonp with varnish


I try to configure varnish so it can handle jsonp.
I use as the basic concept this source: https://gist.github.com/640331

I'm using varnish 3.0.2 so I ended up with this

After sending a request to the server:

curl http://example.com/test?callback=test

I get the following:

test(<esi:include src="http://example.com/test" />);

Insted of:

test({...cached-json...});

The syslog output:

The syslog outputs the following:
May 09 12:48:45 [varnishd] before if for: /JSONP-ESI-TEMPLATE
May 09 12:48:45 [varnishd] before if for: callback, url: example.com:6081
May 09 12:48:45 [varnishd] in if for: callback, url: example.com:6081
May 09 12:52:40 [varnishd] before if for: /JSONP-ESI-TEMPLATE
May 09 12:52:40 [varnishd] in if for: /JSONP-ESI-TEMPLATE
May 09 12:52:40 [varnishd] before if for: obj.status == 760
May 09 12:52:40 [varnishd] in if for: obj.status == 760
May 09 12:52:40 [varnishd] before if for: beresp.http.X-ESI
May 09 12:52:40 [varnishd] in if for: beresp.http.X-ESI
May 09 12:52:40 [varnishd] before if for: beresp.http.X-JSONP-Server
May 09 12:52:40 [varnishd] in if for: beresp.http.X-JSONP-Server

How to evaluate the esi request to get the json?


Solution

  • Got the solution! Looks like this.

    If You are using gzip for Your json responses, don't forget to enable gzip for jsonp:

    if ( beresp.http.content-type ~ "application/javascript" ) { 
      set beresp.do_gzip = true;
    }