0

I have installed Varnish on my Linux server and configured for my websites including a wordpress site (www.mywordpress.com) and it is working fine. Now I have installed mantis bug tracker under my website (www.mywordpress.com/mantis). But when I am trying to login to the MantisBT as the default user (administrator/root), it shows an error like "Your browser either does not know how to handle cookies, or refuses to handle them". How can I set up a Varnish exception or allow cookies (in default.vcl) for Mantis url's. My default.vcl file looks like:


###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;

}

}

1 Answers1

2

First, change this, it is unsetting any cookie which not inside wp-login or wp-admin:

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

to something like this:

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

(Where '||' means OR, '~' means equals to about, and 'req.url' - the requested URL)

and in vcl_recv (no matter where, put it in the beggining), ignore caching /mantis URLs:

sub vcl_recv {

    ...

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

    ...
}

and restart varnish (usually sudo service varnish restart on ubuntu). check again and it should be fine (If its not working, clean your browser's cookies and cache).

...And, why is mantis not inside the wp-admin directory? is it a wordpress plugin?

smartDonkey
  • 550
  • 1
  • 5
  • 14