Possible allow cookies for wp-admin, but not front end while using Varnish?

I’m running Varnish in front of my WordPress site. To increase my cache hit stats I blocked cookies in my varnish configuration in default.vcl

# Drop any cookies sent to WordPress.
sub vcl_recv {
    unset req.http.cookie;
}
# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
    unset beresp.http.set-cookie;
}

I believe this is what is keeping me from logging in to wp-admin right now. I am continually redirected back to the login page. Any idea what kind of filter I need to pass into that cookie blocker? I’m not familiar with this configuration language.

Related posts

Leave a Reply

2 comments

  1. You need to NOT drop cookies if the URL is from the admin section.

    Like this:

    sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
            unset req.http.cookie;
        }
    }
    
    sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
            unset beresp.http.set-cookie;
        }
    }
    
  2. Why not:

    sub vcl_recv {
        if (req.http.Cookie ~ "(wordpress_|wp-)") {
            return (pass); // If WP cookies exist, do not cache
        } else {
            unset req.http.Cookie;
        }
    }
    

    ?