Getting Varnish To Work on Magento

Getting Varnish To Work on Magento

August 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Getting Varnish To Work on Magento

Hello developers! Today we’re going to tackle an important topic that’s often overlooked - optimizing your Magento store performance using Varnish cache. We’ll be looking at a common challenge many of you might face when setting up Varnish for the first time.

The Challenge: Cookies and Varnish Incompatibility

When configuring Varnish with your Magento store, you may encounter issues due to the cookies set by Magento itself. By default, Magento sets a session cookie that includes sensitive information like user authentication status.

The issue arises because Varnish, out of the box, does not cache responses containing cookies. This means that even though you’ve installed and started Varnish according to the guides (like this one), your store might still not be fully cached.

Let’s first understand the cookie being set by Magento:

Set-Cookie: frontend=6t2d2q73rv9s1kddu8ehh8hvl6; expires=Thu, 17-Feb-2011 14:29:19 GMT; path=/; domain=XX.X.XX.XX; httponly

As you can see:

  • It’s a session cookie (frontend)
  • It has an expiration date and time
  • The domain is set to the IP address of your server

Configuring Varnish to Handle Cookies

To get Varnish to cache Magento responses despite this cookie, we need to tell Varnish to ignore it. Here’s how:

Step 1: Update Your vcl_recv Function

Open your default.vcl file (usually located in the /etc/varnish directory). In the vcl_recv function, add a new condition to ignore the specific cookie:

if (req.http.cookie ~ "frontend=") {
    return(synth(403, "Not allowed"));
}

This tells Varnish to discard any requests that contain the frontend cookie before they reach Magento.

Step 2: Update Your vcl_fetch Function

Next, in the vcl_fetch function, remove the Set-Cookie header from the response:

remove headers Set-Cookie;

This ensures that Varnish doesn’t forward the original cookie to clients when serving cached responses.

Conclusion

Configuring Varnish to handle cookies is a critical step in optimizing your Magento store’s performance. By ignoring the frontend session cookie, we allow Varnish to cache anonymous user requests effectively.

Remember, this approach only works for anonymous users. For logged-in users or those with persistent sessions, you’ll need to use a more complex setup involving subdomains or Varnish’sESI functionality.

For a detailed guide on Magento performance tuning using Varnish, check out our comprehensive guide at Laravel Company.

Happy caching! Don’t hesitate to reach out if you have any further questions.

Cache Performance Configuration Import/Export

Our network