【问题标题】:Varnish cookie issue with mantis bug tracker螳螂错误跟踪器的清漆cookie问题
【发布时间】:2014-07-07 16:17:27
【问题描述】:

我已经在我的 Linux 服务器上安装了 Varnish 并为我的网站进行了配置,包括一个 wordpress 网站 (www.mywordpress.com),它运行良好。现在我已经在我的网站 (www.mywordpress.com/mantis) 下安装了 mantis 错误跟踪器。但是,当我尝试以默认用户(管理员/root)身份登录 MantisBT 时,它会显示一个错误,例如“您的浏览器要么不知道如何处理 cookie,要么拒绝处理它们”。如何为 Mantis url 设置 Varnish 异常或允许 cookie(在 default.vcl 中)。我的 default.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;

}

}

【问题讨论】:

    标签: linux wordpress cookies varnish mantis


    【解决方案1】:

    首先,改变这个,它正在取消设置不在 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;
    }
    

    (其中 '||' 表示 OR,'~' 表示等于 about,'req.url' - 请求的 URL)

    并且在vcl_recv(不管在哪里,放在开头),忽略缓存/mantis URLs:

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

    并重新启动 varnish(通常在 ubuntu 上为 sudo service varnish restart)。再次检查应该没问题(如果它不起作用,请清理浏览器的 cookie 和缓存)。

    ...而且,为什么 mantis 不在 wp-admin 目录中?是wordpress插件吗?

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多