0

I'm trying to reduce the number of requests' connections of js scripts and one option is usage of esi tag.

Like from: <script src="https://whatever.min.js"></script>

To: <script><esi:include src="https://whatever.min.js"/></script> But now cdn is giving 503 error for it and trying to treat script like page block. Doesn't work for fastly and varnish

  • Please provide your full VCL file as well as the `varnishlog` output for that page. The command you run to display the logs is `varnishlog -g request -q "RespStatus == 503"` – Thijs Feryn Oct 25 '22 at 14:17
  • @ThijsFeryn I'm afraid I cannot share the full VCL, sorry. Based on my findings and attempts to solve it I can say that varnish sends request for esi part to internal apache, apache treats that as proper request and send back with redirect code. Unfortunately varnish receives only redirect code and doesn't follow to the content of it. It can follow (retry request) with special code snippet, but request gets into the loop because request goes into apache :) I'm thinking about apache-way solutions because it's most safe way. Thank a lot for your ideas – Gosponemo Oct 29 '22 at 22:33

2 Answers2

0

NOTE: Ideally you should share a bit more of your VCL code so we can see which VCL subroutine you're triggering the esi statement from and understand what else might be affecting the ESI parser from being executed.

But just to clarify, in case other readers are unfamiliar, that ESI tags should be placed within HTML content (i.e. your origin server would serve HTML containing <esi:> tags) and Varnish will be instructed that the response needs to have those ESI tags parsed/fetched.

Varnish will then make separate sequential fetches for each esi:include tag it encounters and ensure the response is placed in the relevant location of the HTML that is served to the client.

Here is an example that is using ESI (it's built with Fastly's Fiddle tool): https://fiddle.fastly.dev/fiddle/f2842f47

You'll need to execute the esi statement to let Varnish know that the ESI parser is required.

I would recommend you read Fastly's ESI reference page: https://developer.fastly.com/reference/vcl/statements/esi/

If you require any additional support please contact support@fastly.com

Thanks!


Other reference material:

Integralist
  • 495
  • 3
  • 7
  • Hi. Thank you for your answer. Maybe you know if it is possible to limit amount of ESI request? I mean cache all page and do esi requests only after a certain number of request (something like TTL) – Gosponemo Nov 05 '22 at 21:22
  • Hi I don't believe this is possible. I believe you would need to set a short lived TTL for the esi instead. – Integralist Nov 07 '22 at 09:40
0

You're right, Varnish doesn't follow redirects while processing ESI tags.

Here's some code you could add to your VCL to mitigate this:

sub vcl_deliver {
    if (((resp.status == 301) || (resp.status == 302)) && req.esi_level > 0) {
        set req.url = regsub(resp.http.Location,"^https?://[^/]+(.*)","\1");
        return(restart);
    }
}
Thijs Feryn
  • 3,982
  • 1
  • 5
  • 10