keepcalculator

06 Numbers and Powers

Bypass Varnish Cache for Logged-In Admins in WordPress

Card No. 06·72004 · Updated · Standard formula, shown below

When you run WordPress behind Varnish, logged-in admins often see stale pages or get logged out because Varnish serves cached copies to everyone, including you. This guide shows you how to configure Varnish to bypass the cache for logged-in users, so you always see fresh content while visitors still get the speed boost.

What You’ll Need

  • Root or sudo access to your server (to edit Varnish configuration files).
  • Access to your WordPress installation files (to edit functions.php or a custom plugin).
  • Varnish installed and running (version 4 or 5 commonly used).
  • Basic familiarity with the command line and VCL syntax.

Step-by-Step

  1. Identify your Varnish configuration file. On most systems, it’s located at /etc/varnish/default.vcl or /etc/varnish/varnish.vcl. Open it with a text editor (e.g., sudo nano /etc/varnish/default.vcl).
  2. Add a cookie-based bypass in vcl_recv. Find the sub vcl_recv block. Add a condition that detects a logged-in WordPress user by checking for the wordpress_logged_in cookie. For example:
    if (req.http.cookie ~ "wordpress_logged_in") {
        return (pass);
    }

    This tells Varnish to skip the cache and pass the request directly to the backend.

  3. Also bypass for admin pages. Even if the cookie isn’t present, you may want to bypass cache for /wp-admin and /wp-login.php. Add:
    if (req.url ~ "^/wp-(login|admin)") {
        return (pass);
    }

    Place this before the cookie check to ensure admin functions always run fresh.

  4. Handle the vcl_backend_response for Set-Cookie. In sub vcl_backend_response, you should not cache responses that contain Set-Cookie headers, because that would break sessions. Add:
    if (beresp.http.Set-Cookie) {
        set beresp.ttl = 0s;
    }

    This ensures any page that sets a cookie (like after login) is never cached.

  5. Strip cookies for static files (optional but recommended). To improve cache hit rates for images, CSS, and JS, you can remove cookies for those requests. In vcl_recv, add:
    if (req.url ~ "\.(css|js|png|gif|jpg|jpeg|ico|svg|woff2?)$") {
        unset req.http.cookie;
    }

    This does not affect admin bypass because admin URLs don’t match the static file pattern.

  6. Reload Varnish. After saving the VCL file, test the configuration with varnishd -C -f /etc/varnish/default.vcl (or the path to your config). If no errors, reload Varnish with sudo systemctl reload varnish (or sudo service varnish reload on older systems).
  7. Verify the bypass. Log in to WordPress as an admin, then check the response headers. Use curl -I http://yourdomain.com/wp-admin/ (or your admin URL) and look for X-Varnish and Via headers. If the request shows X-Varnish: without a second number (like 123 456), it means the request was not served from cache. Also, make a test post and confirm it appears immediately when you refresh the front end.

Common Mistakes

  • Using the wrong cookie name. WordPress uses wordpress_logged_in_ followed by a hash, but the cookie name always starts with wordpress_logged_in. Your regex should match the prefix, not the full name.
  • Forgetting to handle Set-Cookie in backend responses. If you don’t set TTL to 0 for responses with Set-Cookie, Varnish may cache a page that contains a session cookie, causing logged-out users to see admin-specific content.
  • Placing the bypass after a generic return (hash). In VCL, once you return, the rest of the subroutine is ignored. Make sure your bypass checks come before any unconditional return (hash) or return (lookup).
  • Not reloading Varnish properly. A syntax error in VCL will prevent Varnish from starting. Always test the config before reloading.
  • Ignoring other cookies. If you use a caching plugin that sets its own cookies, you might need to adjust the bypass to include those as well. But be careful not to bypass for all cookies, or you’ll defeat caching.

Tips

  • Use a custom header to debug. In vcl_deliver, you can add a header like set resp.http.X-Cache = "HIT" or "MISS" to make it easy to see if a page was cached. This is helpful during testing.
  • Consider using a WordPress plugin for cache control. Some plugins (like WP Rocket or W3 Total Cache) have built-in Varnish integration, but they may not handle the admin bypass automatically. Your VCL change is still the most reliable method.
  • Test with an incognito window. After logging out, open a private browsing window to ensure that the cache is still working for visitors. Log in again in a normal window to confirm the bypass.
  • Keep your VCL version in mind. Varnish 4 and 5 use return (pass), but Varnish 6+ uses return (pass) as well, so the syntax remains similar. However, some functions changed; always check your Varnish version documentation.

Troubleshooting

  • Varnish fails to start after editing VCL. Run varnishd -C -f /etc/varnish/default.vcl to see the exact syntax error. Common issues include missing semicolons, incorrect braces, or using a function not available in your Varnish version.
  • Admin still sees cached pages. Check if the cookie is actually being set. Use your browser’s developer tools to view cookies. If the cookie name is different (e.g., wordpress_logged_in_abcdef123), your regex ~ "wordpress_logged_in" should still match because it’s a substring. If not, adjust the pattern to include the underscore.
  • Logged-out users see admin content. This usually means a page with Set-Cookie was cached. Ensure you added the TTL=0 rule in vcl_backend_response and that you reloaded Varnish.
  • Varnish is not bypassing even after reload. Verify that the VCL file you edited is the one actually loaded. Check the running config with varnishadm vcl.list and varnishadm vcl.show active to see the active configuration.
  • Performance drops for anonymous users. If you’ve stripped cookies too aggressively, you might be caching pages that should not be cached (e.g., pages with dynamic content). Review your static file regex and make sure you only strip cookies for assets, not for HTML pages.

Frequently Asked Questions

Will this affect my page speed for visitors?

No. The bypass only applies to requests that have the wordpress_logged_in cookie or hit /wp-admin or /wp-login.php. Anonymous visitors won’t have that cookie, so they’ll still get cached pages.

Do I need to modify WordPress code as well?

No, the VCL changes are sufficient. WordPress already sets the wordpress_logged_in cookie when a user logs in, so Varnish can detect it without any PHP changes.

What if I use a different caching plugin?

Most caching plugins work with Varnish, but some set their own cookies (like wp-settings-*). The bypass for logged-in users is independent of the plugin, so it should still work. Just make sure your plugin doesn’t override the VCL or set a different cookie for logged-in users.

Can I bypass cache for other user roles (editors, authors) too?

Yes, the cookie is the same for any logged-in user, so the bypass applies to all logged-in users, not just admins. If you want to allow caching for non-admin logged-in users, you’d need a more complex check, but that’s rarely needed.

Is it safe to use return (pass) for all admin pages?

Yes, admin pages are not meant to be cached because they contain dynamic data and session information. Passing them to the backend ensures they always reflect the latest state.

How do I check if the bypass is working?

Use curl -I http://yourdomain.com/wp-admin/ with your admin session cookie. Look for the X-Varnish header. If it shows a single number (e.g., 123456), it means the request was passed (not cached). If it shows two numbers (e.g., 123456 789012), it was served from cache, and your bypass isn’t working.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →