清漆4,缓存页面与cookie

I want to cache every page (even with cookies), backend in php is not prepared for this, so it's sending multiple Set-Cookie headers. When I remove all session_start and setcookie funcitons, the page HITs, but when I set my vcl to varnish do it automatically for me, page allways MISSes.

Here is my default.vcl:

vcl 4.0;

backend default {
    .host = "myhost.app";
    .port = "80";
}

sub vcl_recv {
    unset req.http.cookie;
}

sub vcl_backend_response {
    unset beresp.http.set-cookie;
}

sub vcl_deliver {
    if (obj.hits > 0) {
       set resp.http.X-Cache = "HIT";
    } else {
       set resp.http.X-Cache = "MISS";
    }
}

It seems you are going into muddy waters, but try forcing a ttl:

sub vcl_backend_response {
   set beresp.ttl = 120 s;
   unset beresp.http.set-cookie;
}

Check that you are not breaking things by caching ignoring cookies and ttls.

I was also facing cache MISS every time and updated sub vcl_backend_response{..} as mentioned by Jorge Nerín. Now It was showing cache HIT, but login was not happening. It was obvious because of unset beresp.http.set-cookie;. So I get idea from Manipulating request headers in VCL to unset cookie based on URL. I applied same invcl_backend_response. In my app login URL contains auth. So I updated to unset beresp.http.set-cookie; every where except when URL contain auth. Now login is happening and also cache HIT. Hope this helps community

sub vcl_backend_response {
 set beresp.ttl = 120 s;
 if ((bereq.url ~ "auth")){
  }
 else{
    unset beresp.http.set-cookie;
 }
 #rest of code
}