Php Varnish一直缺少缓存,cookies?

Php Varnish一直缺少缓存,cookies?,php,varnish,Php,Varnish,我在正确缓存清漆时遇到了一些问题 当我转到一个页面并点击刷新时,varnish将返回缓存的页面。 但是,如果我使用另一台计算机并再次点击同一页(在缓存时间范围内),它将报告未命中 我使用了 我从(php)页面中删除了session_start(),不需要基于用户的cookies。 我也在使用谷歌分析 这是我默认的.vcl ################################################### # Copyright (c) UNIXY - http://www

我在正确缓存清漆时遇到了一些问题

当我转到一个页面并点击刷新时,varnish将返回缓存的页面。 但是,如果我使用另一台计算机并再次点击同一页(在缓存时间范围内),它将报告未命中

我使用了
我从(php)页面中删除了session_start(),不需要基于用户的cookies。
我也在使用谷歌分析

这是我默认的.vcl

###################################################
# Copyright (c) UNIXY  -  http://www.unixy.net    #
# The leading truly fully managed server provider #
###################################################

include "/etc/varnish/cpanel.backend.vcl";

include "/etc/varnish/backends.vcl";

sub vcl_recv {

# Use the default backend for all other requests
set req.backend = default;

# Setup the different backends logic
include "/etc/varnish/acllogic.vcl";

# Allow a grace period for offering "stale" data in case backend lags
set req.grace = 5m;

remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;

# cPanel URLs
include "/etc/varnish/cpanel.url.vcl";

# Properly handle different encoding types
if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|ico)$") {
        # No point in compressing these
        remove req.http.Accept-Encoding;
    } elsif (req.http.Accept-Encoding ~ "gzip") {
        set req.http.Accept-Encoding = "gzip";
    } elsif (req.http.Accept-Encoding ~ "deflate") {
        set req.http.Accept-Encoding = "deflate";
    } else {
        # unkown algorithm
        remove req.http.Accept-Encoding;
    }
}

# Set up disabled
include "/etc/varnish/disabled.vcl";

# Exclude upgrade, install, server-status, etc
include "/etc/varnish/known.exclude.vcl";

# Set up exceptions
include "/etc/varnish/url.exclude.vcl";

# Set up exceptions
include "/etc/varnish/vhost.exclude.vcl";

# Set up vhost+url exceptions
include "/etc/varnish/vhosturl.exclude.vcl";

# Set up cPanel reseller exceptions
include "/etc/varnish/reseller.exclude.vcl";

# Restart rule for bfile recv
include "/etc/varnish/bigfile.recv.vcl";


if (req.request == "PURGE") {
        if (!client.ip ~ acl127_0_0_1) {error 405 "Not permitted";}
        return (lookup);
}

## Default request checks
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
    return (pipe);
}

if (req.request != "GET" && req.request != "HEAD") {
    return (pass);
}

## Modified from default to allow caching if cookies are set, but not http auth
if (req.http.Authorization) {
    return (pass);
}

include "/etc/varnish/versioning.static.vcl";

## Remove has_js and Google Analytics cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");

set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");



if (req.http.Cookie ~ "^\s*$") {
    unset req.http.Cookie;
}

include "/etc/varnish/slashdot.recv.vcl";

# Cache things with these extensions
if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|pdf)$" && ! (req.url ~ "\.(php)") ) {
    unset req.http.Cookie;
    return (lookup);
}

return (lookup);
}

sub vcl_fetch {

set beresp.ttl = 40s;
set beresp.http.Server = " - Web acceleration by http://www.unixy.net/varnish ";

# Turn off Varnish gzip processing
include "/etc/varnish/gzip.off.vcl";

# Grace to allow varnish to serve content if backend is lagged
set beresp.grace = 5m;

# Restart rule bfile for fetch
include "/etc/varnish/bigfile.fetch.vcl";

# These status codes should always pass through and never cache.
if (beresp.status == 503 || beresp.status == 500) {
    set beresp.http.X-Cacheable = "NO: beresp.status";
    set beresp.http.X-Cacheable-status = beresp.status;
    return (hit_for_pass);
}

if (beresp.status == 404) {
    set beresp.http.magicmarker = "1";
    set beresp.http.X-Cacheable = "YES";
    set beresp.ttl = 20s;
    return (deliver);
}

/* Remove Expires from backend, it's not long enough */    
unset beresp.http.expires;

if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|pdf|ico)$" && ! (req.url ~ "\.(php)") ) {
    unset beresp.http.set-cookie;
    include "/etc/varnish/static.ttl.vcl";
}
include "/etc/varnish/slashdot.fetch.vcl"; 
else {
    include "/etc/varnish/dynamic.ttl.vcl";
}

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

# All tests passed, therefore item is cacheable
set beresp.http.X-Cacheable = "YES";

return (deliver);
}

sub vcl_deliver {

  # From http://varnish-cache.org/wiki/VCLExampleLongerCaching
  if (resp.http.magicmarker) {
     /* Remove the magic marker */
     unset resp.http.magicmarker;

     /* By definition we have a fresh object */
     set resp.http.age = "0";
   }

   #add cache hit data
   if (obj.hits > 0) {
     #if hit add hit count
     set resp.http.X-Cache = "HIT";
     set resp.http.X-Cache-Hits = obj.hits;
   }
else {
     set resp.http.X-Cache = "MISS";
   }

}

sub vcl_error {

if (obj.status == 503 && req.restarts < 5) {
set obj.http.X-Restarts = req.restarts;
return (restart);
}

}

# Added to let users force refresh
sub vcl_hit {

if (obj.ttl < 1s) {
    return (pass);
}

if (req.http.Cache-Control ~ "no-cache") {
# Ignore requests via proxy caches,  IE users and badly behaved crawlers
# like msnbot that send no-cache with every request.
if (! (req.http.Via || req.http.User-Agent ~ "bot|MSIE|HostTracker")) {
    set obj.ttl = 0s;
    return (restart);
} 
}

return (deliver);

}

sub vcl_hash {

    hash_data(req.http.cookie);
}
有人能给我解释一下为什么会发生这种情况,以及我应该改变什么吗


谢谢

这是因为谷歌分析。即使您的Web应用程序不使用cookies,您的分析JavaScript也会使用cookies。结果是一样的,Varnish会将请求传递到后端,并避免使用它的缓存

要解决此问题,请定义您的Web应用程序的URL,其中您必须使用cookie(例如管理面板),而您不需要使用cookie(此处您可以忽略Google Analytics的要求。大多数Web分析工具只需要浏览器和JavaScript之间的cookie)

下面,您可以看到一个清漆配置文件的示例。有趣的是,对于网站的非管理部分:发送的cookie以及一些新内容的浏览器请求标题将被删除

sub vcl_recv {
  # regex to find all URLs where cookies are required
  if (req.url ~ "^/admin/") {
    # administration panel
    set req.http.admin = 1;
  } else {
    # public web site, ignore client request for fresh content, remove cookies
    unset req.http.Cache-Control;
    unset req.http.Max-Age;
    unset req.http.Pragma;
    unset req.http.Cookie;
  }
  ...
}

sub vcl_fetch {
  if (req.http.admin == 1) {
    # administration panel
    return (hit_for_pass);
  } else {
    # public web site, not allowed to set cookies
    unset beresp.http.Set-Cookie;
    ...
  }
  ...
}

您可以使用
varnishlog
命令行工具来观察Varnish在处理每个请求时正在做什么。例如,您可以查看是否有任何cookie正在拆分

我感觉可能是另一台计算机上的用户代理稍有不同。Varnish在键中包含此值(如果已知)。我们的
vcl\u recv
中有一条规则:

set req.http.User-Agent = "";

这为我们解决了一个类似的问题。

默认情况下,以下代码是否不处理google cookie?#Remove拥有_js和Google Analytics Cookie。设置req.http.Cookie=regsuball(req.http.Cookie,(^ |;\s*)(uuu[a-z]+| has_js)=[^;]*,”);从外观上看,这确实是一个有效的正则表达式来去除那些cookies。但从清漆日志的输出来看,这并没有发生。有没有可能在其中一个包含中触发了返回(pass)或返回(pipe)?您已经粘贴了响应头,但我缺少一些包含脚本设置的expire值。我假设ttl设置为默认的40秒。也许在vcl_fetch中禁用magicmarker是个好主意,并在进行一些测试时观察年龄的变化。没有包含返回(仅适用于cpanel/whm URL)。没有区别。。可能是什么…:(在这个解决方案中,IE浏览器可能会有问题。。如果(req.http.User-Agent~“MSIE”){set req.http.User-Agent=“MSIE”;}或者{set req.http.User-Agent=“Mozilla compatible”;}对不起,请用英语说
set req.http.User-Agent = "";