WordPress.org

Forums

WooCommerce - excelling eCommerce
Varnish configuration not working in Woocommerce (9 posts)

  1. jacob.tingen
    Member
    Posted 1 year ago #

    I've followed the steps here and read through other forums/guides I found online. The only reason I'm starting a new topic instead of adding to existing topics on this same question is because I have already tried everything in other topics with no luck.

    I fixed the issue in the second clause and placed the escape character before the "?".

    Nothing I do seems to work--the cart, checkout, and my account pages do not behave the way they are supposed to. Here is what I have in my varnish default.vcl file:

    # Drop any cookies sent to WordPress.
    sub vcl_recv {
            if (!(req.url ~ "wp-(login|admin)")) {
                    unset req.http.cookie;
            }
    if (req.url ~ "^/(cart|my-account|checkout|addons)") {
     return (pass);
     }
    if (req.url ~ "\?add-to-cart=" ) {
     return (pass);
     }
    }

    What am I missing?

    https://wordpress.org/plugins/woocommerce/

  2. jacob.tingen
    Member
    Posted 1 year ago #

    Nevermind, I started googling a little more in-depth on making Varnish work with WordPress in general instead of making Varnish work with WooCommerce specifically.

    The code I originally posted above was a mix of what I got from two different sources.

    The first part was the code I got from Digital Ocean:

    # Drop any cookies sent to WordPress.
    sub vcl_recv {
            if (!(req.url ~ "wp-(login|admin)")) {
                    unset req.http.cookie;
            }
    }

    This second part was what I got from the WooCommerce documentation:

    if (req.url ~ "^/(cart|my-account|checkout|addons)") {
     return (pass);
     }
    if (req.url ~ "\?add-to-cart=" ) {
     return (pass);
     }

    I realized the two bits of code conflict--the first snippet prevented WooCommerce from using any cookies on any page at all except for wp-login and wp-admin. That's when I realized I could just customize that snippet to except WooCommerce pages too.

    The following code works for me. It still prevents the use of many WordPress cookies, so it still leverages Varnish to cache most of my website (I read that WP's use of cookies often prevents Varnish from actually caching anything unless setup appropriately). Here's the code that worked for me:

    # Drop any cookies sent to WordPress.
    sub vcl_recv {
            if (!(req.url ~ "(wp-login|wp-admin|cart|my-account|checkout|addons)")) {
                    unset req.http.cookie;
            }
    
    # Drop any cookies WordPress tries to send back to the client.
    sub vcl_fetch {
            if (!(req.url ~ "(wp-login|wp-admin|cart|my-account|checkout|addons)")) {
                    unset beresp.http.set-cookie;
            }
    }

    Here is what I read that got me to my answer: https://www.varnish-cache.org/trac/wiki/VarnishAndWordpress

    Just posting in case it helps anyone else. There might be a better way to do this, but it seems to be working fine.

  3. whereskarlo
    Member
    Posted 1 year ago #

    Fantastic.. I will try this and report back. I tried getting Varnish to work many times without success.

  4. whereskarlo
    Member
    Posted 1 year ago #

    This code does not work for me.

    I get an error.

    root@wordpress:~# service varnish start
     * Starting HTTP accelerator varnishd                                    [fail]
    Message from VCC-compiler:
    Expected an action, 'if', '{' or '}'
    ('input' Line 25 Pos 1)
    sub vcl_fetch {
    ###------------
    
    Running VCC-compiler failed, exit 1
    
    VCL compilation failed
  5. mpeimpiii
    Member
    Posted 4 months ago #

    I'm kind of sad to see a "Member Since: January 15th, 2015"
    discouraging the community research in favor of a post created a couple of hours ago.

    I Just think it would be more fair if you published their part of implementation which answers the community's questions (with the backlink, that's fair), not just a backlink.

  6. lickthespoon
    Member
    Posted 4 months ago #

    I'm curious playwithux as I have just to move with cloudways and Woocommerce/varnish. I had lots of problems with the Ajax add to cart particularly with the sidebar cart widget. They were very helpful and solved the problem... But I'm not entirely convinced Varnish is actually working as I see zero hits to the cache on their monitoring making me think it's bypassed under the hood. The hosting seems quite fast with other caching such as Apc and I'm live so I don't want to play too much and risk crashing the site functionality.
    So do you think that Varnish is working with Woocommerce with your sites on cloudways including cart widgets etc? I wonder if they announced it too soon without testing the various widget combinations.
    Unfortunately the actual varnish setups are hidden as it's a sort of managed hosting.

  7. lickthespoon
    Member
    Posted 3 months ago #

    Hi playwithUX,
    Thank you for the information. Do you use a sidebar cart widget... does that work correctly? If so would you mind sharing your site url?
    If it seems to work well (I won't try and break your site honest) just want to compare with my site http://www.lickthespoon.co.uk

    Thank you,

    Matthew

  8. whereskarlo
    Member
    Posted 3 months ago #

    I ended up switching to CloudWays too after successfully migrating and testing my website with Varnish, MemCached and everything else working flawlessly. Page load times were a little slower than Rackspace but the caching counters it. I also pay $30 instead of ~$60 at RS. If I go up one level I get better performance than RS for $75 and finally not having to dabble with the back-end anymore. I also switched because of running the latest Php which I needed for a plugin and updating that on RS turned out to be a pain.

    To be fair I hate the fact that I ended up getting swooned over to CloudWays. However, I was getting real tired of pretending to be a sys-admin and Rackspace requires a minimum of $500 per month to get their managed services level (correct me if I'm wrong) which would have probably attempted to fix Varnish for me and installing the latest Php version etc. I was spending more time in the back-end than on WooCommerce and it actually feels nice to only worry about my website while knowing that the rest is taken care of without spending serious dough. I think CloudWays was designed specifically for WordPress + WooCommerce which is why it works for me.

    I can't access root so I can't tell which settings they used for Varnish. But cart works and sessions work. The only thing that broke my site was activating W3 Total Cache as it messed up a custom plugin I have.

  9. lickthespoon
    Member
    Posted 3 months ago #

    I contacted support and after a couple of trys they've now apparently updated the VCL and woocommerce is now working with Varnish at Cloudways for me. I'll keep testing and see how it goes. I have quite a lot of plugins so it does seem configurations can vary with different cookies. W3 Total Cache is enabled and they use some of the settings to avoid caching the cart, My Account pages etc I'm very pleased with their support. They do seem to have an in depth knowledge of wordpress and woocommerce and are willing to go into admin and check W3TC setup etc.
    Not much use to anybody trying to set up their own Varnish suffice to say it can be done and I'm not sure exactly what they've done to get it working - sorry!
    I'm not sure how much performance difference it will make as it was pretty nippy without varnish. They also have APC Page cache and Database cache set up in W3TC but Object cache is off.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic