2

I have a PHP page that I cache with Varnish: http://categories.php?name=NAME. It has some ESI scripts like : < esi:include src="/esi.php" / >

All the pages have similar header, footer, sidebars so I remove the name=NAME when doing the VCL hash so all pages can get the same cache.

The problem is I would like inside the esi.php script to know what page issued the ESI request. Was it categories.php?name=A or categories.php?name=B ?

How can I do this? I didn't find this information anywhere.

I was thinking it might be inside the HTTP REFERER, but it isn't. :(

vladady
  • 21
  • 3

3 Answers3

2

Same problem, same status. To solve it I added the parent URL as a parameter to the ESI URL:

<esi:include src="/esi/content/?url=http://mysite.com/parent/page/" />
Jens Bradler
  • 1,407
  • 12
  • 11
1

Try this in vcl_recv:

if (req.esi_level == 0) {
    set req.http.X-Esi-Parent = req.url;
}   

This will result in all esi requests related to this main request sending the original url in a request header called "X-Esi-Parent" which can be read on the back end.

Update

Aleksandr in the comments correctly points out that this won't work, since headers set in vcl_recv are not present on subsequent esi requests -- only headers from the client. A few people have asked similar questions on the listserv without being answered:

https://www.varnish-cache.org/lists/pipermail/varnish-misc/2012-February/021706.html

http://marc.info/?l=varnish-dev&m=135273366517518&w=2

So it seems there is no way to do this if you want to cache the response that includes the esi tags across all query string combinations.

Johnny C
  • 1,799
  • 1
  • 16
  • 27
  • This doesn't work. Varnish will try new request for ESI. All request headers that are set during original request will be lost. – Aleksandar Janković Aug 02 '13 at 19:36
  • Hmm this did work for me, with two modifications: conditional should be `req.esi_level != 0`, and instead of `req.url` get the enclosing page's URL from `req_top.url`. – Andrew Mar 13 '22 at 23:21
1

Starting from Varnish 4.1 you can do:

 if (0 < req.esi_level && req_top.url ~ "found/in/parent/url") {
     ...
 }