Here is the varnish-cache.org example of using Varnish as a proxy for cross domain XHR for your ajax/javascript apps to get get around XMLHTTPRequest's cross-domain limitations.
http://www.varnish-cache.org/trac/wiki/VCLExampleXMLHTTPRequest
First, add a backend for whatever service you want to masquerade
backend masq {
.host = "masqueraded.example.com";
.port = "80";
}
Then map it into your own URL space:
sub vcl_recv {
/* Rewrite all requests to /masq to <a href="http://masqueraded.example.com/cgi-bin/foo.cgi" title="http://masqueraded.example.com/cgi-bin/foo.cgi">http://masqueraded.example.com/cgi-bin/foo.cgi</a> */
if (req.url ~ "^/masq") {
set req.http.host = "masq.example.com";
set req.url = regsub(req.url, "^/masq", "/cgi-bin/foo.cgi");
set req.backend = masq;
remove req.http.Cookie;
return(lookup);
}
In some cases, you might need to fix up the response, if so, do something like
sub vcl_fetch {
if (req.http.host == "masq.example.com") {
set obj.http.Content-Type = "text/html; charset=utf-8"; /* Correct the wrong response */
set obj.ttl = 86400s;
set obj.http.Cache-Control = "max-age=3600";
return(deliver);
}
Post new comment