简体   繁体   中英

Varnish seems not to be caching pages at all

I have a PHP Symfony application which is served by nginx .

*   << BeReq    >> 492062
-   Begin          bereq 492061 fetch
-   Timestamp      Start: 1572337898.474535 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /
-   BereqProtocol  HTTP/1.0
-   BereqHeader    Host: xxx
-   BereqHeader    X-Forwarded-Host: xxx
-   BereqHeader    X-Real-IP: xxx
-   BereqHeader    X-Forwarded-Proto: https
-   BereqHeader    HTTPS: on
-   BereqHeader    User-Agent: Wget/1.19.4 (linux-gnu)
-   BereqHeader    Accept: */*
-   BereqHeader    X-Forwarded-For: 127.0.0.1
-   BereqProtocol  HTTP/1.1
-   BereqHeader    Accept-Encoding: gzip
-   BereqHeader    X-Varnish: 492062
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   BackendOpen    26 boot.default 127.0.0.1 8080 127.0.0.1 43676
-   BackendStart   127.0.0.1 8080
-   Timestamp      Bereq: 1572337898.474685 0.000150 0.000150
-   Timestamp      Beresp: 1572337903.642006 5.167471 5.167321
-   BerespProtocol HTTP/1.1
-   BerespStatus   200
-   BerespReason   OK
-   BerespHeader   Server: nginx/1.14.0 (Ubuntu)
-   BerespHeader   Content-Type: text/html; charset=UTF-8
-   BerespHeader   Transfer-Encoding: chunked
-   BerespHeader   Connection: keep-alive
-   BerespHeader   Set-Cookie: PHPSESSID=slaurqvo3msh9uklerbht0nd2h; path=/; domain=.xxx; HttpOnly
-   BerespHeader   Cache-Control: max-age=3600, public
-   BerespHeader   Date: Tue, 29 Oct 2019 08:31:39 GMT
-   BerespHeader   Age: 20
-   BerespHeader   Content-Encoding: gzip
-   TTL            RFC 3600 10 0 1572337904 1572337884 1572337899 0 3600
-   VCL_call       BACKEND_RESPONSE
-   TTL            VCL 86420 10 0 1572337884
-   TTL            VCL 86420 3600 0 1572337884
-   TTL            VCL 140 3600 0 1572337884
-   VCL_return     deliver
-   BerespHeader   Vary: Accept-Encoding
-   Storage        malloc Transient
-   ObjProtocol    HTTP/1.1
-   ObjStatus      200
-   ObjReason      OK
-   ObjHeader      Server: nginx/1.14.0 (Ubuntu)
-   ObjHeader      Content-Type: text/html; charset=UTF-8
-   ObjHeader      Set-Cookie: PHPSESSID=slaurqvo3msh9uklerbht0nd2h; path=/; domain=.xxx; HttpOnly
-   ObjHeader      Cache-Control: max-age=3600, public
-   ObjHeader      Date: Tue, 29 Oct 2019 08:31:39 GMT
-   ObjHeader      Content-Encoding: gzip
-   ObjHeader      Vary: Accept-Encoding
-   Fetch_Body     2 chunked stream
-   Gzip           u F - 24261 118266 80 80 194017
-   BackendReuse   26 boot.default
-   Timestamp      BerespBody: 1572337903.644744 5.170209 0.002738
-   Length         24261
-   BereqAcct      275 0 275 342 24261 24603
-   End

*   << Request  >> 492061
-   Begin          req 492060 rxreq
-   Timestamp      Start: 1572337898.474380 0.000000 0.000000
-   Timestamp      Req: 1572337898.474380 0.000000 0.000000
-   ReqStart       127.0.0.1 57354
-   ReqMethod      GET
-   ReqURL         /
-   ReqProtocol    HTTP/1.0
-   ReqHeader      Host: xxx
-   ReqHeader      X-Forwarded-Host: xxx
-   ReqHeader      X-Real-IP: xxx
-   ReqHeader      X-Forwarded-For: xxx
-   ReqHeader      X-Forwarded-Proto: https
-   ReqHeader      HTTPS: on
-   ReqHeader      Cache-Control: max-age=15000
-   ReqHeader      Connection: close
-   ReqHeader      User-Agent: Wget/1.19.4 (linux-gnu)
-   ReqHeader      Accept: */*
-   ReqHeader      Accept-Encoding: identity
-   ReqUnset       X-Forwarded-For: xxx
-   ReqHeader      X-Forwarded-For: xxx, 127.0.0.1
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: xxx, 127.0.0.1
-   ReqHeader      X-Forwarded-For: 127.0.0.1
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: identity
-   VCL_call       HASH
-   VCL_return     lookup
-   HitMiss        492059 104.991348
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 492062 fetch
-   Timestamp      Fetch: 1572337903.643494 5.169113 5.169113
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx/1.14.0 (Ubuntu)
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     Set-Cookie: PHPSESSID=slaurqvo3msh9uklerbht0nd2h; path=/; domain=.xxx; HttpOnly
-   RespHeader     Cache-Control: max-age=3600, public
-   RespHeader     Date: Tue, 29 Oct 2019 08:31:39 GMT
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     X-Varnish: 492061
-   RespHeader     Age: 20
-   RespHeader     Via: 1.1 varnish (Varnish/5.2)
-   VCL_call       DELIVER
-   VCL_return     deliver
-   Timestamp      Process: 1572337903.643520 5.169140 0.000026
-   RespUnset      Content-Encoding: gzip
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Connection: close
-   Gzip           U D - 24261 118266 80 80 194017
-   Timestamp      Resp: 1572337903.645192 5.170812 0.001672
-   ReqAcct        313 0 313 381 118266 118647
-   End

*   << Session  >> 492060
-   Begin          sess 0 HTTP/1
-   SessOpen       127.0.0.1 57354 a0 127.0.0.1 80 1572337898.474305 24
-   Link           req 492061 rxreq
-   SessClose      TX_EOF 5.171
-   End

Somehow, Varnish seems to save the website in cache:

-   VCL_call       DELIVER
-   VCL_return     deliver

VCL Configuration:

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    // Remove all cookies except the session ID.
    if (req.http.Cookie) {
        set req.http.Cookie = ";" + req.http.Cookie;
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1=");
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

        if (req.http.Cookie == "") {
            // If there are no more cookies, remove the header to get page cached.
            unset req.http.Cookie;
        }
    }
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.

    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}
sub vcl_deliver
{
    # Insert Diagnostic header to show Hit or Miss
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
        set resp.http.X-Cache-Hits = obj.hits;
    }
    else {
        set resp.http.X-Cache = "MISS";
    }
}

What is wrong there?

You shall not cache pages with a Set-Cookie Header!

also you are quoting the wrong lines to determine whether it was cached or not: VCL_call MISS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM