www.fgks.org   »   [go: up one dir, main page]

Varnish cache as an AJAX cross-domain XHR proxy

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

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.
Bg