我已经在我的Linux服务器上安装了Varnish,并为我的网站(包括wordpress网站(www.my wordpress.com))进行了配置,运行良好。现在我已经在我的网站(www.mywordpress.com/mantis)下安装了mantis bug tracker。但当我试图以默认用户(管理员/根用户)身份登录MantisBT时,它会显示一个错误,如“您的浏览器不知道如何处理cookies,或者拒绝处理cookies”。如何设置Varnish异常或允许Mantis url的Cookie(默认为.vcl)。我的默认.vcl文件如下所示:

###my default.vcl file:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
backend master {
.host = "127.0.0.1";
.port = "8080";
}
acl purge {
    "localhost";
}
sub vcl_recv {
if (req.request == "PURGE") {
    if (!client.ip ~ purge) {
        error 405 "Not allowed.";
    }
    return(lookup);
}
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;
    }
}


### do not cache these files:
if (req.url ~ "/svn" || req.http.Authorization || req.http.Authenticate)
{
    return (pass);
}

##never cache the admin pages, or the server-status page
if (req.url ~ "wp-(admin|login)" || req.http.Content-Type ~ "multipart/form-data")
{
    set req.backend = master;
    return(pass);
}

if (req.url ~ "opportunity-attachments" || req.http.Content-Type ~ "multipart/form-data")
{
    set req.backend = master;
    return(pass);
}

if (req.url ~ "^phpmyadmin") {
    set req.backend = master;
    return(pipe);
}

if (req.url ~ "^/login") {
    set req.backend = master;
    return(pipe);
}

## always cache these images & static assets
if (req.request == "GET" && req.url ~ "\.(css|js|gif|jpg|jpeg|bmp|png|ico|img|tga|wmf)$") {
    remove req.http.cookie;
    return(lookup);
}
if (req.request == "GET" && req.url ~ "(xmlrpc.php|wlmanifest.xml)") {
    remove req.http.cookie;
    return(lookup);
}

#never cache POST requests
if (req.request == "POST")
{
    return(pass);
}
#DO cache this ajax request
if(req.http.X-Requested-With == "XMLHttpRequest" && req.url ~ "recent_reviews")
{
    return (lookup);
}

#dont cache ajax requests
if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)")
{
    return (pass);
}

if (req.http.Cookie && req.http.Cookie ~ "wordpress_") {
    set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=", "; wpjunk=");
}
### don't cache authenticated sessions
if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID)") {
    return(pass);
}

### parse accept encoding rulesets to make it look nice
if (req.http.Accept-Encoding) {
    if (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;
    }
}


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, ";(vendor_region|PHPSESSID|themetype2)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
}

if (req.url ~ "^/$") {
    unset req.http.cookie;
}
return(lookup);
}

sub vcl_hit {
if (req.request == "PURGE") {
    set obj.ttl = 0s;
    error 200 "Purged.";
 }
}
sub vcl_miss {
if (req.request == "PURGE") {
    error 404 "Not in cache.";
}
if (!(req.url ~ "wp-(login|admin)")) {
    unset req.http.cookie;
}

if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
    unset req.http.cookie;
    set req.url = regsub(req.url, "\?.$", "");
}
if (req.url ~ "^/$") {
    unset req.http.cookie;
}

}
sub vcl_fetch {
if (req.url ~ "^/$") {
    unset beresp.http.set-cookie;
}
if (!(req.url ~ "wp-(login|admin)")) {
    unset beresp.http.set-cookie;

}

}

最佳答案

首先,更改此设置,它将取消wp login或wp admin中不包含的任何cookie:

if (!(req.url ~ "wp-(login|admin)")) {
    unset req.http.cookie;
}

像这样的:
if (!(req.url ~ "wp-(login|admin)") || !(req.url ~ "mantis")) {
    unset req.http.cookie;
}

(其中,“| |”表示或,“~”表示大约,而“req.url”-请求的url)
在vcl_recv中(无论在哪里,将其放入begging),忽略缓存/mantisurl:
sub vcl_recv {

    ...

    if (req.url ~ "/mantis")
    {
        return (pass);
    }

    ...
}

重新启动varnish(在ubuntu上通常sudo service varnish restart)。再检查一次,应该没问题(如果不工作,请清理浏览器的cookies和缓存)。
…还有,为什么mantis不在wp管理目录中?是wordpress插件吗?

09-07 14:42