当 Nginx 作为后端服务器时,Varnish 不断出现 MISS

当 Nginx 作为后端服务器时,Varnish 不断出现 MISS

我遇到了一个非常奇怪的情况,Varnish 可以直接连接到应用程序服务器(Play 服务器),但不能与 Nginx 作为 Web 服务器一起使用。

  • 工作:清漆:6081 => 播放:9000
  • 不起作用:Varnish:6081=> Nginx:80(反向代理上游到 Play)=> Play:9000

Nginx 配置非常简单

    location / {
    <% if env !~ /prod/ -%>
        ssi on; #turn on ssi
        proxy_set_header Accept-Encoding ""; # turn on the gzip in the request head request to Play, so the gz can be handled on Nginx
    <% end -%>

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_pass_header True-Client-IP;
        proxy_pass_header NSC-client-ip;

        proxy_pass         http://playservers;
        proxy_read_timeout 120s;
    }

不工作的 Varnish 请求日志

*   << Request  >> 262151    
-   Begin          req 98305 rxreq
-   Timestamp      Start: 1430506271.730155 0.000000 0.000000
-   Timestamp      Req: 1430506271.730155 0.000000 0.000000
-   ReqStart       172.29.225.140 59335
-   ReqMethod      GET
-   ReqURL         /en/store-finder
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: sdc-d1-caredesicache-app2.dev.walmart.com:6081
-   ReqHeader      Connection: keep-alive
-   ReqHeader      Pragma: no-cache
-   ReqHeader      Cache-Control: no-cache
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   ReqHeader      User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
-   ReqHeader      Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
-   ReqHeader      Cookie: AMCV_C4C6370453309C960A490D44%40AdobeOrg=1766948455%7CMCMID%7C53747178867536931389166539357943008388%7CMCAAMLH-1430926858%7C7%7CMCAAMB-1430926858%7CNRX38WO0n5BH8Th-nqAG_A%7CMCAID%7CNONE; __gads=ID=21e2f1aff97858dc:T=1430322062:S=ALNI_MbzvCyNpyNFP
-   ReqHeader      X-Forwarded-For: 172.29.225.140
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: 172.29.225.140
-   ReqHeader      X-Forwarded-For: 172.29.225.140
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   Debug          "XXXX HIT-FOR-PASS"
-   HitPass        2147483654
-   VCL_call       PASS
-   VCL_return     fetch
-   Link           bereq 262152 pass
-   Timestamp      Fetch: 1430506272.045614 0.315459 0.315459
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx
-   RespHeader     Date: Fri, 01 May 2015 18:51:11 GMT
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Access-Control-Allow-Credentials: true
-   RespHeader     Access-Control-Allow-Origin: http://pstore-dev2.walmart.ca
-   RespHeader     Request-Id: 588
-   RespHeader     Request-Time: 5ms
-   RespHeader     Served-By: app2
-   RespHeader     X-Frame-Options: SAMEORIGIN
-   RespHeader     X-Page-Type: storeFinder
-   RespHeader     X-UA-Compatible: IE=edge,chrome=1
-   RespHeader     Set-Cookie: ENV=pstore-dev;max-age=1200;path=/;domain=.walmart.ca
-   RespHeader     Timing-Allow-Origin: *
-   RespHeader     Content-Encoding: gzip
-   RespHeader     cache-control: max-age=900
-   RespHeader     magicmarker: 1
-   RespHeader     X-Varnish: 262151
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   RespHeader     X-Age: 0
-   RespUnset      Age: 0
-   RespHeader     X-Cache: MISS
-   VCL_return     deliver
-   Timestamp      Process: 1430506272.045706 0.315551 0.000092
-   RespHeader     Transfer-Encoding: chunked
-   Debug          "RES_MODE 8"
-   RespHeader     Connection: keep-alive
-   RespHeader     Accept-Ranges: bytes
-   Timestamp      Resp: 1430506276.767342 5.037187 4.721636
-   Debug          "XXX REF 1"
-   ReqAcct        1808 0 1808 675 178859 179534
-   End            
**  << BeReq    >> 262152    
--  Begin          bereq 262151 pass
--  Timestamp      Start: 1430506271.730440 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       /en/store-finder
--  BereqProtocol  HTTP/1.1
--  BereqHeader    Host: sdc-d1-caredesicache-app2.dev.walmart.com:6081
--  BereqHeader    Pragma: no-cache
--  BereqHeader    Cache-Control: no-cache
--  BereqHeader    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
--  BereqHeader    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
--  BereqHeader    Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
--  BereqHeader    Cookie: AMCV_C4C6370453309C960A490D44%40AdobeOrg=1766948455%7CMCMID%7C53747178867536931389166539357943008388%7CMCAAMLH-1430926858%7C7%7CMCAAMB-1430926858%7CNRX38WO0n5BH8Th-nqAG_A%7CMCAID%7CNONE; __gads=ID=21e2f1aff97858dc:T=1430322062:S=ALNI_MbzvCyNpyNFP
--  BereqHeader    X-Forwarded-For: 172.29.225.140
--  BereqHeader    Accept-Encoding: gzip
--  BereqHeader    X-Varnish: 262152
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  Backend        19 vdir play2(10.242.215.40,,80)
--  Timestamp      Bereq: 1430506271.730848 0.000407 0.000407
--  Timestamp      Beresp: 1430506271.784953 0.054513 0.054105
--  BerespProtocol HTTP/1.1
--  BerespStatus   200
--  BerespReason   OK
--  BerespHeader   Server: nginx
--  BerespHeader   Date: Fri, 01 May 2015 18:51:11 GMT
--  BerespHeader   Content-Type: text/html; charset=utf-8
--  BerespHeader   Transfer-Encoding: chunked
--  BerespHeader   Connection: keep-alive
--  BerespHeader   Keep-Alive: timeout=20
--  BerespHeader   Vary: Accept-Encoding
--  BerespHeader   Access-Control-Allow-Credentials: true
--  BerespHeader   Access-Control-Allow-Origin: http://pstore-dev2.walmart.ca
--  BerespHeader   Request-Id: 588
--  BerespHeader   Request-Time: 5ms
--  BerespHeader   Served-By: app2
--  BerespHeader   X-Frame-Options: SAMEORIGIN
--  BerespHeader   X-Page-Type: storeFinder
--  BerespHeader   X-UA-Compatible: IE=edge,chrome=1
--  BerespHeader   Set-Cookie: ENV=pstore-dev;max-age=1200;path=/;domain=.walmart.ca
--  BerespHeader   Timing-Allow-Origin: *
--  BerespHeader   Content-Encoding: gzip
--  TTL            RFC 3600 -1 -1 1430506272 1430506272 1430506271 0 0
--  VCL_call       BACKEND_RESPONSE
--  TTL            VCL 86400 10 0 1430506272
--  BerespHeader   cache-control: max-age=900
--  BerespHeader   magicmarker: 1
--  TTL            VCL 120 10 0 1430506272
--  VCL_return     deliver
--  Storage        malloc Transient
--  ObjProtocol    HTTP/1.1
--  ObjStatus      200
--  ObjReason      OK
--  ObjHeader      Server: nginx
--  ObjHeader      Date: Fri, 01 May 2015 18:51:11 GMT
--  ObjHeader      Content-Type: text/html; charset=utf-8
--  ObjHeader      Vary: Accept-Encoding
--  ObjHeader      Access-Control-Allow-Credentials: true
--  ObjHeader      Access-Control-Allow-Origin: http://pstore-dev2.walmart.ca
--  ObjHeader      Request-Id: 588
--  ObjHeader      Request-Time: 5ms
--  ObjHeader      Served-By: app2
--  ObjHeader      X-Frame-Options: SAMEORIGIN
--  ObjHeader      X-Page-Type: storeFinder
--  ObjHeader      X-UA-Compatible: IE=edge,chrome=1
--  ObjHeader      Set-Cookie: ENV=pstore-dev;max-age=1200;path=/;domain=.walmart.ca
--  ObjHeader      Timing-Allow-Origin: *
--  ObjHeader      Content-Encoding: gzip
--  ObjHeader      cache-control: max-age=900
--  ObjHeader      magicmarker: 1
--  Fetch_Body     2 chunked -
--  Gzip           U F - 175253 2677572 80 1173648 1401956
--  Gzip           G F E 2677572 178843 80 1430664 1430674
--  BackendReuse   19 play2(10.242.215.40,,80)
--  Timestamp      BerespBody: 1430506272.046118 0.315678 0.261165
--  Length         178843
--  BereqAcct      1821 0 1821 568 175312 175880
--  End  

在职的

*   << Request  >> 32780     
-   Begin          req 1 rxreq
-   Timestamp      Start: 1430506376.837363 0.000000 0.000000
-   Timestamp      Req: 1430506376.837363 0.000000 0.000000
-   ReqStart       172.29.225.140 59394
-   ReqMethod      GET
-   ReqURL         /en/store-finder
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: sdc-d1-caredesicache-app2.dev.walmart.com:6081
-   ReqHeader      Connection: keep-alive
-   ReqHeader      Pragma: no-cache
-   ReqHeader      Cache-Control: no-cache
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   ReqHeader      User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
-   ReqHeader      Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
-   ReqHeader      Cookie: AMCV_C4C6370453309C960A490D44%40AdobeOrg=1766948455%7CMCMID%7C53747178867536931389166539357943008388%7CMCAAMLH-1430926858%7C7%7CMCAAMB-1430926858%7CNRX38WO0n5BH8Th-nqAG_A%7CMCAID%7CNONE; __gads=ID=21e2f1aff97858dc:T=1430322062:S=ALNI_MbzvCyNpyNFP
-   ReqHeader      X-Forwarded-For: 172.29.225.140
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: 172.29.225.140
-   ReqHeader      X-Forwarded-For: 172.29.225.140
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   Hit            2147516419
-   VCL_call       HIT
-   VCL_return     deliver
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Access-Control-Allow-Credentials: true
-   RespHeader     Access-Control-Allow-Origin: http://pstore-dev2.walmart.ca
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Request-Id: 592
-   RespHeader     Request-Time: 5ms
-   RespHeader     Served-By: app2
-   RespHeader     X-Frame-Options: SAMEORIGIN
-   RespHeader     X-Page-Type: storeFinder
-   RespHeader     X-UA-Compatible: IE=edge,chrome=1
-   RespHeader     Date: Fri, 01 May 2015 18:52:42 GMT
-   RespHeader     cache-control: max-age=900
-   RespHeader     magicmarker: 1
-   RespHeader     Content-Encoding: gzip
-   RespHeader     X-Varnish: 32780 32771
-   RespHeader     Age: 15
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   RespHeader     X-Age: 15
-   RespUnset      Age: 15
-   RespHeader     X-Cache: HIT
-   VCL_return     deliver
-   Timestamp      Process: 1430506376.837544 0.000181 0.000181
-   RespHeader     Content-Length: 178843
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: keep-alive
-   RespHeader     Accept-Ranges: bytes
-   Timestamp      Resp: 1430506376.837811 0.000448 0.000267
-   Debug          "XXX REF 2"
-   ReqAcct        1808 0 1808 547 178843 179390
-   End 

这是我的 default.vcl

vcl 4.0;

import std;

# Default backend definition. Set this to point to your content server.
include "backends.vcl";

acl purge {
  "127.0.0.1";
  "localhost";
}

sub vcl_recv {
     # Allow purging
     if (req.method == "PURGE") {
        if (!client.ip ~ purge) { # purge is the ACL defined at the begining
          # Not from an allowed IP? Then die with an error.
          return (synth(405, "This IP is not allowed to send PURGE requests."));
        }
        # If you got this stage (and didn't error out above), purge the cached result
        return (purge);
    }

    set req.backend_hint = vdir.backend();
    set req.http.X-Forwarded-For = client.ip;

    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    if (req.url ~ "\.(gif|png|bmp|ico|jpeg|jpg)$" || // static image files
        req.url ~ "(?i)\.(aif|aiff|au|avi|bin|cab|carb|cct|cdf|class|css|doc|dcr|dtd|exe|flv|gcf|grv|hdml|hqx|ini|js|mov|mp3|nc|pct|pdf|ppc|pws|swa|swf|txt|vbs|w32|wav|wbmp|wml|wmlc|wmls|wmlsc|xsd|zip)$" || // other static assets
        req.url ~ "^/(en|fr)$" || // landing pages
        req.url ~ "^/(en/store-finder|fr/trouver-magasin)")
        {
            return(hash);
        }
    return(pass);
}

sub vcl_hash {

    #hash_data(req.http.cookie);

    ## defaults
    if ( req.url ~ "^/(en|fr)(\?[^/]+){0,1}$" ){
        hash_data(regsub(req.url, "^/(en|fr)(\?[^/]+){0,1}$", "/\1"));
    } else {
        hash_data(req.url);
    }
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    ## end of defaults

   return(lookup);
}

sub vcl_hit {
  # Called when a cache lookup is successful.

  if (obj.ttl >= 0s) {
    # A pure unadultered hit, deliver it
    return (deliver);
  }

  # We have no fresh fish. Lets look at the stale ones.
  if (std.healthy(req.backend_hint)) {
    # Backend is healthy. Limit age to 10s.
    if (obj.ttl + 10s > 0s) {
      #set req.http.grace = "normal(limited)";
      return (deliver);
    } else {
      # No candidate for grace. Fetch a fresh object.
      return(fetch);
    }
  } else {
    # backend is sick - use full grace
      if (obj.ttl + obj.grace > 0s) {
      #set req.http.grace = "full";
      return (deliver);
    } else {
      # no graced object.
      return (fetch);
    }
  }

  # fetch & deliver once we get the result
  return (fetch); # Dead code, keep as a safeguard
}

sub vcl_miss {
  # Called after a cache lookup if the requested document was not found in the cache. Its purpose
  # is to decide whether or not to attempt to retrieve the document from the backend, and which
  # backend to use.

  return (fetch);
}


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.
    set beresp.do_esi = true; // Do ESI processing
    set beresp.do_gzip = true;

    if (bereq.url ~ "^/(en|fr)(\?[^/]+){0,1}$" || bereq.url ~ "^/(en|fr)$") {
        set beresp.ttl = 4h;
    }
    if (bereq.url ~ "^/(en/store-finder|fr/trouver-magasin)"){
        set beresp.ttl = 1d;
    }
    if (beresp.ttl > 0s) {
                        /* Remove Expires from backend, it's not long enough */
                        unset beresp.http.expires;

                        /* Set the clients TTL on this object */
                        set beresp.http.cache-control = "max-age=900";

                        /* marker for vcl_deliver to reset Age: */
                        set beresp.http.magicmarker = "1";
    }
}

sub vcl_deliver {
    set resp.http.X-Age = resp.http.Age;
    unset resp.http.Age;

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

    return (deliver);

}

相关内容