Mal
Mal

Reputation: 334

How to include content from other side with ESI in Varnish

I want to use ESI in Varnish to combine content from different sides. Every side is a small micro service with a small frontend snippet. ESI should construct the page with the different snippets.

I'll be using Varnish 4.0.5. As long I'll use it for content from my side its works fine.

<html>
<body>
    <esi:include src="/hello"/>                 <!-- works -->
    <esi:include src="http://www.example.org/index.html"/>   <!-- doesn't works -->
</body>
</html>

Here's my vcl

vcl 4.0;
backend default {
    .host = "localhost";
    .port = "8080";
}

sub vcl_recv {

    # Only a single backend
    set req.backend_hint= default;

    # Setting http headers for backend
    set req.http.X-Forwarded-For = client.ip;

    # Unset headers that might cause us to cache duplicate infos
    unset req.http.Accept-Language;
    unset req.http.User-Agent;


    # drop cookies and params from static assets
    if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
      unset req.http.cookie;
      set req.url = regsub(req.url, "\?.*$", "");
    }

    # drop tracking params
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
      set req.url = regsub(req.url, "\?.*$", "");
   }
}
sub vcl_backend_response { 
  set beresp.do_esi = true;  
}    

I'll get the following result in the browser

hello
Cannot GET /index.html

When I define the external host also in the VCL

backend otherbackend {
    .host = "www.example.org";
    .port = "80";
}

and

sub vcl_recv {
    # Only a single backend
    set req.backend_hint= default;

    if (req.http.host == "www.example.org") {
      set req.backend_hint = otherbackend;
    }

I'll get some content from the external site (static assets will no be served and therefore leads to an error in the browser)

Question - Is there a way to get content from an external site without defining every external site as a backend?

Upvotes: 2

Views: 859

Answers (0)

Related Questions