Mailing List Archive

Set cache headers for browsers by varnish.
Hi,


Is it possible for varnish to send the "Cache-Control: no-cache, no-store,
must-revalidate" cache control response to browser, while varnish caches
the response.


Scenario is like this.

1) Backend sends cache control :Cache-Control: no-cache, no-store,
must-revalidate
2) Varnish should cache the response.
3) Browser should not cache the contents so in response from varnish it
should show
cache control :Cache-Control: no-cache, no-store, must-revalidate


I have tried using Cache-Control: no-cache, no-store, must-revalidate in
set beresp.http.Cache-Control, but this causes varnish not to cache the
responses.


Given below is the vcl_backend_response
=============================
sub vcl_backend_response {
if (bereq.url == "/") {

unset beresp.http.expires;
unset beresp.http.set-cookie;
set beresp.ttl = 3600s;
set beresp.http.Cache-Control = "max-age=0";
#Prevent caching of 404 and 500 errors
if (beresp.status >= 400 && beresp.status <= 599) {
set beresp.ttl = 0s;
}
}
}
=============================

Any help is highly appreciated.
Re: Set cache headers for browsers by varnish. [ In reply to ]
Easy fix is: set it in vcl_deliver instead of vcl_backend_response.

Have a look at builtin.vcl (varnishadm vcl.show -v boot) that gets
automatically appended to your vcl. If you wish to bypass it,
return(deliver) from your code and the built-in code won't be ran.

--
Guillaume Quintard

On May 9, 2017 14:00, "sreeranj s" <sreeranj4droid@gmail.com> wrote:

> Hi,
>
>
> Is it possible for varnish to send the "Cache-Control: no-cache, no-store,
> must-revalidate" cache control response to browser, while varnish caches
> the response.
>
>
> Scenario is like this.
>
> 1) Backend sends cache control :Cache-Control: no-cache, no-store,
> must-revalidate
> 2) Varnish should cache the response.
> 3) Browser should not cache the contents so in response from varnish it
> should show
> cache control :Cache-Control: no-cache, no-store, must-revalidate
>
>
> I have tried using Cache-Control: no-cache, no-store, must-revalidate in
> set beresp.http.Cache-Control, but this causes varnish not to cache the
> responses.
>
>
> Given below is the vcl_backend_response
> =============================
> sub vcl_backend_response {
> if (bereq.url == "/") {
>
> unset beresp.http.expires;
> unset beresp.http.set-cookie;
> set beresp.ttl = 3600s;
> set beresp.http.Cache-Control = "max-age=0";
> #Prevent caching of 404 and 500 errors
> if (beresp.status >= 400 && beresp.status <= 599) {
> set beresp.ttl = 0s;
> }
> }
> }
> =============================
>
> Any help is highly appreciated.
>
>
>
>
>
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>