Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux varnish 4缓存期限始终为零_Linux_Apache_Amazon Web Services_Varnish - Fatal编程技术网

Linux varnish 4缓存期限始终为零

Linux varnish 4缓存期限始终为零,linux,apache,amazon-web-services,varnish,Linux,Apache,Amazon Web Services,Varnish,我在一个varnish实例上使用varnish 4,它指向三个apache Web服务器,但是浏览时的缓存时间始终为0 以下是配置, default.vcl vcl 4.0; import std; import directors; # This is a basic VCL configuration file for varnish. See the vcl(7) # man page for details on VCL syntax and semantics. # # Def

我在一个varnish实例上使用varnish 4,它指向三个apache Web服务器,但是浏览时的缓存时间始终为0

以下是配置, default.vcl

vcl 4.0;
import std;
import directors;

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.
# 


include "backends.vcl";

sub vcl_recv {

    set req.backend_hint = vdir.backend();
    # Authentication



    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.method != "GET" &&
      req.method != "HEAD" &&
      req.method != "PUT" &&
      req.method != "POST" &&
      req.method != "TRACE" &&
      req.method != "OPTIONS" &&
      req.method != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }
    if (req.method != "GET" && req.method != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }

    # checks if the url is a static content, if yes, returns (lookup)
    if (
        #homepage
        req.url ~ "^(/.{2})?/$"

        # ads page
        || req.url ~ "^(/.{2})?/(rent|buy|commercial)/([a-z]+\-)+([0-9]+)\.html$"

        || req.url ~ "^/ajax/.{2}/ads/suggest.*"
        || req.url ~ "^/.{2}/privacy-policy.html"
        || req.url ~ "^/.{2}/terms-and-conditions.html"
        || req.url ~ "^/.{2}/about-us.html"
        || req.url ~ "^(/.{2})?/search-form"
        || req.url ~ "^(/.{2})?/category-homepage/(.{1})$"
        || req.url ~ "^/ajax/(.{2})?/ads/locations\?l=(.{0,6})$"

    ) {
        return (hash);
    }

    return (pass);
}

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_pass {
    return (fetch);
}

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

     if (req.http.X-Forwarded-Proto) {
        hash_data(req.http.X-Forwarded-Proto);
     }

    call cookie_hash;

    return (lookup);
}

sub cookie_hash {
    set req.http.pf-tab-cookie = "";
    if (req.http.Cookie ~ ".*pf-tab-cookie=([^;]+)(;.*)?$") {
        set req.http.pf-tab-cookie = regsub(req.http.Cookie, ".*pf-tab-cookie=([^;]+)(;.*)?$", "\1");
    }

    # For search form ESI tag
    if (req.url ~ "^(/.{2})?/search-form") {
        set req.http.pf-search-form-values = "default";
        if (req.http.Cookie ~ ".*pf-search-form-values=([^;]+)(;.*)?$") {
            set req.http.pf-search-form-values = regsub(req.http.Cookie, ".*pf-search-form-values=([^;]+)(;.*)?$", "\1");
        }
        hash_data(req.http.pf-tab-cookie + req.http.pf-search-form-values);
        unset req.http.pf-search-form-values;
   }

    # "home" route
    if (req.url ~ "^(/.{2})?/category-homepage/(.{1})$") {
        hash_data(req.http.pf-tab-cookie);
    }

    unset req.http.pf-tab-cookie;
}

sub vcl_hit {
    return (deliver);
}

sub vcl_miss {
    return (fetch);
}

sub vcl_backend_response {
    // The application controls if the page shouldn't be cached
    if (beresp.http.X-Varnish-pass) {
        set beresp.uncacheable = true;

        return  (deliver);
    }

    set beresp.ttl = 5m;

    // Homepage and ads page
    if (
        bereq.url ~ "^(/.{2})?/$"

        # ads page
        || bereq.url ~ "^(/.{2})?/(rent|buy|commercial)/([a-z]+\-)+([0-9]+)\.html$"
    ) {
        set beresp.do_esi = true;
        set beresp.ttl = 10m;
    }

    // Static pages
    if (
        bereq.url ~ "^/.{2}/privacy-policy.html"
        || bereq.url ~ "^/.{2}/terms-and-conditions.html"
        || bereq.url ~ "^/.{2}/about-us.html"
    ) {
        set beresp.do_esi = true;
        set beresp.ttl = 1d;
    }

    // Search form
    if (bereq.url ~ "^(/.{2})?/search-form") {
       set beresp.ttl = 7d;
    }

    // Homepage categories
    if (bereq.url ~ "^(/.{2})?/category-homepage/(.{1})$") {
        set beresp.ttl = 10m;
    }

    // Location ajax form
    if (bereq.url ~ "^/ajax/(.{2})?/ads/locations\?l=(.{0,6})$") {
        set beresp.ttl = 1d;
    }

    if (beresp.status >= 400 && beresp.status < 600) {
        set beresp.ttl = 1s;
        set beresp.uncacheable = true;

        return  (deliver);
    }

    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;
        # set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return (deliver);
    }
    if (beresp.status >= 400 && beresp.status < 600) {
       set beresp.uncacheable = true;
    }

    return (deliver);
}

sub vcl_deliver {
    return (deliver);
}

sub vcl_backend_error {
    set beresp.http.Content-Type = "text/html; charset=utf-8";
    set beresp.http.Retry-After = "5";
    synthetic({"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>"} + beresp.status + " " + beresp.reason + {"</title>
  </head>
  <body>
    <h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
    <p>"} + beresp.reason + {"</p>
    <h3>HAKEEM Meditation:</h3>
    <p>XID: "} + bereq.xid + {"</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>
"});
    return (deliver);
}

sub vcl_synth {
   if (resp.status == 401) {
       set resp.http.WWW-Authenticate = "Basic";
   } else {

        set resp.http.Content-Type = "text/html; charset=utf-8";
        set resp.http.Retry-After = "5";
        synthetic({"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>"} + resp.status + " " + resp.reason + {"</title>
  </head>
  <body>
    <h1>Error "} + resp.status + " " + resp.reason + {"</h1>
    <p>"} + resp.reason + {"</p>
    <h3>HAKEEM :</h3>
    <p>XID: "} + req.xid + {"</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>
"});
    }
    return (deliver);
}

sub vcl_init {
        call backends_init;
}

sub vcl_fini {
    return (ok);

如果响应头包含缓存控制:private varnish将不会缓存。 看

backend i_c38f540c_10_20_0_171 {
    .host = "10.20.0.171";

        .port = "80";
        .probe = {
                .request = "HEAD /robots.txt HTTP/1.1" "Host: mydomain.com" "Authorization: Basic dfsJdsdfsfsfdsmV3ZsdfsdfsVy" "Connection: close";
                .threshold = 1;
                .window = 2;
                .timeout = 5s;
                .initial = 1;
                .expected_response = 301;
                .interval = 5s;
        }
        .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
    .connect_timeout        = 10s;     # How long to wait for a backend connection?
    .between_bytes_timeout  = 10s;     # How long to wait between bytes received from our backend?

}
backend i_6975c6e7_10_20_0_176 {
    .host = "10.20.0.176";

        .port = "80";
        .probe = {
                .request = "HEAD /robots.txt HTTP/1.1" "Host: mydomain.com" "Authorization: Basic dfsJdsdfsfsfdsmV3ZsdfsdfsVy" "Connection: close";
                .threshold = 1;
                .window = 2;
                .timeout = 5s;
                .initial = 1;
                .expected_response = 301;
                .interval = 5s;
        }
        .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
    .connect_timeout        = 10s;     # How long to wait for a backend connection?
    .between_bytes_timeout  = 10s;     # How long to wait between bytes received from our backend?

}
backend i_f668db78_10_20_0_135 {
    .host = "10.20.0.135";

        .port = "80";
        .probe = {
                .request = "HEAD /robots.txt HTTP/1.1" "Host: mydomain.com" "Authorization: Basic dfsJdsdfsfsfdsmV3ZsdfsdfsVy" "Connection: close";
                .threshold = 1;
                .window = 2;
                .timeout = 5s;
                .initial = 1;
                .expected_response = 301;
                .interval = 5s;
        }
        .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
    .connect_timeout        = 10s;     # How long to wait for a backend connection?
    .between_bytes_timeout  = 10s;     # How long to wait between bytes received from our backend?

}


sub backends_init {
    new vdir = directors.round_robin();

    vdir.add_backend(i_c38f540c_10_20_0_171);
    vdir.add_backend(i_6975c6e7_10_20_0_176);
    vdir.add_backend(i_f668db78_10_20_0_135);
}