对于有效的 URL,Varnish 返回 404

标签 varnish varnish-vcl

我有一个Varnish设置,可以将某些特定的URL(主要是XML输出)缓存5-10秒。 DC家伙设置了它并创建了配置文件,但是不幸的是它无法正常工作。

如果您能看一下配置并帮助我发现问题,我将非常高兴。

这是我的default.vcl

backend default {
    .host = "127.0.0.1";
    .port = "80";
  }

  sub vcl_recv {
       if (req.restarts == 0) {
         if (req.http.x-forwarded-for) {
             set req.http.X-Forwarded-For =
                 req.http.X-Forwarded-For + ", " + client.ip;
         } else {
             set req.http.X-Forwarded-For = client.ip;
         }
       }

       if (req.request != "GET" &&
         req.request != "HEAD" &&
         req.request != "PUT" &&
         req.request != "POST" &&
         req.request != "TRACE" &&
         req.request != "OPTIONS" &&
         req.request != "DELETE") {
           /* Non-RFC2616 or CONNECT which is weird. */
           return (pipe);
       }
       if (req.request != "GET" && req.request != "HEAD") {
           /* We only deal with GET and HEAD by default */
           return (pass);
       }
       if (req.http.Authorization || req.http.Cookie) {
           /* Not cacheable by default */
           return (pass);
       }

       if(req.http.host == "foo.example1.com"){
         if(req.url ~ "^/somedir\?somevar=anothervar&andanother=.*"){
           return (lookup);
         }
       }else if(req.http.host == "bar.example2.com"){
         if(req.url ~ "^/foo/bar\?somevar=anothervar&andanother=.*"){
           return (lookup);
         }
       } else {
        return (pass);
       }

       # Currently not reached.
       return (lookup);
  }

  sub vcl_pipe {
      # Note that only the first request to the backend will have
      # X-Forwarded-For set.  If you use X-Forwarded-For and want to
      # have it set for all requests, make sure to have:
      # set bereq.http.connection = "close";
      # here.  It is not set by default as it might break some broken web
      # applications, like IIS with NTLM authentication.
      return (pipe);
  }

  sub vcl_hash {
      hash_data(req.url);
      if (req.http.host) {
          hash_data(req.http.host);
      } else {
          hash_data(server.ip);
      }
      return (hash);
  }

  sub vcl_miss {
      return (fetch);
  }

  sub vcl_fetch {
      if (beresp.ttl <= 0s ||
          beresp.http.Set-Cookie ||
          beresp.http.Vary == "*") {
                /*
                 * Mark as "Hit-For-Pass" for the next 2 minutes
                 */
                set beresp.ttl = 120 s;
                return (hit_for_pass);
      }
      set beresp.ttl = 5 s;
      return (deliver);
  }

以及当我尝试使用localhost调用URL时的日志输出
curl --verbose --header 'Host: foo.example1.com' "http://127.0.0.1:6081/somedir?somevar=anothervar&andanother=123456"是;
   14 BackendClose b default
   14 BackendOpen  b default 127.0.0.1 60502 127.0.0.1 80
   14 TxRequest    b GET
   14 TxURL        b /somedir?somevar=anothervar&andanother=123456
   14 TxProtocol   b HTTP/1.1
   14 TxHeader     b User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.1.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2
   14 TxHeader     b Accept: */*
   14 TxHeader     b Host: foo.example1.com
   14 TxHeader     b X-Forwarded-For: 127.0.0.1
   14 TxHeader     b X-Varnish: 1405453128
   14 TxHeader     b Accept-Encoding: gzip
   14 RxProtocol   b HTTP/1.1
   14 RxStatus     b 404
   14 RxResponse   b Not Found
   14 RxHeader     b Date: Thu, 07 Nov 2013 17:07:50 GMT
   14 RxHeader     b Server: Apache/2.2.15 (Red Hat)
   14 RxHeader     b Content-Length: 295
   14 RxHeader     b Content-Type: text/html; charset=iso-8859-1
   14 Fetch_Body   b 4(length) cls 0 mklen 1
   14 Length       b 295
   14 BackendReuse b default
   12 SessionOpen  c 127.0.0.1 48771 :6081
   12 ReqStart     c 127.0.0.1 48771 1405453128
   12 RxRequest    c GET
   12 RxURL        c /somedir?somevar=anothervar&andanother=123456
   12 RxProtocol   c HTTP/1.1
   12 RxHeader     c User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.1.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2
   12 RxHeader     c Accept: */*
   12 RxHeader     c Host: foo.example1.com
   12 VCL_call     c recv lookup
   12 VCL_call     c hash
   12 Hash         c /somedir?somevar=anothervar&andanother=123456
   12 Hash         c foo.example1.com
   12 VCL_return   c hash
   12 VCL_call     c miss fetch
   12 Backend      c 14 default default
   12 TTL          c 1405453128 RFC 120 -1 -1 1383844070 0 1383844070 0 0
   12 VCL_call     c fetch
   12 TTL          c 1405453128 VCL 5 -1 -1 1383844070 -0
   12 VCL_return   c deliver
   12 ObjProtocol  c HTTP/1.1
   12 ObjResponse  c Not Found
   12 ObjHeader    c Date: Thu, 07 Nov 2013 17:07:50 GMT
   12 ObjHeader    c Server: Apache/2.2.15 (Red Hat)
   12 ObjHeader    c Content-Type: text/html; charset=iso-8859-1
   12 VCL_call     c deliver deliver
   12 TxProtocol   c HTTP/1.1
   12 TxStatus     c 404
   12 TxResponse   c Not Found
   12 TxHeader     c Server: Apache/2.2.15 (Red Hat)
   12 TxHeader     c Content-Type: text/html; charset=iso-8859-1
   12 TxHeader     c Content-Length: 295
   12 TxHeader     c Accept-Ranges: bytes
   12 TxHeader     c Date: Thu, 07 Nov 2013 17:07:50 GMT
   12 TxHeader     c X-Varnish: 1405453128
   12 TxHeader     c Age: 0
   12 TxHeader     c Via: 1.1 varnish
   12 TxHeader     c Connection: keep-alive
   12 Length       c 295
   12 ReqEnd       c 1405453128 1383844070.464804649 1383844070.465569973 0.000082016 0.000713110 0.000052214
   12 SessionClose c EOF
   12 StatSess     c 127.0.0.1 48771 0 1 1 0 0 1 257 295

最佳答案

此 Varnish 日志非常清楚。

您的后端使用404响应您所请求的URL。您将在后端访问日志中找到相同的返回码。

关于对于有效的 URL,Varnish 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19843091/

相关文章:

Varnish 5.2 开始报 "500 Internal Server Error"

c - 内联 C 和 vcl 缓存

caching - Varnish 和 Google Analytics

wordpress - 未经指示时上光缓存wordpress

proxy - 如何在 vcl_recv 中检查后端是否健康

caching - Varnish 4 VCL- strip 定义的查询字符串参数

varnish - 为什么 Varnish 不考虑 malloc 限制?

caching - Varnish可以生成ETag吗?

php - 如果我的网站有很多插件和大数据,如何提高wordpress的速度

varnish - Varnish 默认缓存 404s 吗?