Hello,<br><br>I am evaluating Varnish to put in front of an authenticated web application. Essentially I am trying to get Varnish to make a HEAD request to the back end for authentication before it serves from cache or passes the GET downstream. The web app uses basic auth.<br>
<br>My first attempt seems to work, that is the HEAD request is made and content is cached correctly, until the HEAD to the back end results in a 401. From this point on Varnish always serves the 401 for that request, even after making the HEAD and getting a 2xx from the back end.<br>
<br>I'm pretty sure I am missing something basic as this is my first attempt but some help would be appreciated with my config. The VCL is:<br><br>sub vcl_recv {<br> if (req.request != "GET" &&<br>
.<br>. //from default.vcl<br>.<br>.<br>.// virtual host selection<br>.<br><br> if (req.restarts == 0) {<br> .<br> . // X-forwarded for from default.vcl<br> .<br> if (req.http.Authorization && req.backend == api) {<br>
return(pass);<br> }<br> }<br> return (lookup);<br>}<br><br>sub vcl_pass {<br> if (req.http.Authorization && req.backend == api && req.restarts == 0) {<br> set bereq.request = "HEAD";<br>
}<br> return (pass);<br>}<br><br>sub vcl_fetch {<br> <br> if (req.http.Authorization && req.backend == api && req.restarts == 0) {<br> if (beresp.status >= 200 && beresp.status < 400) {<br>
return(restart);<br> } elsif (beresp.status != 401) {<br> return(error);<br> } else {<br> error 401 "Not Authorised";<br> }<br> } else {<br> if (beresp.ttl <= 0s ||<br>
beresp.http.Set-Cookie ||<br> beresp.http.Vary == "*") {<br> set beresp.ttl = 120 s;<br> return (hit_for_pass);<br> }<br> return (deliver);<br> }<br>
}<br><br>Thanks<br><br>Sumit<br><br>