easyengine icon indicating copy to clipboard operation
easyengine copied to clipboard

Correct Configurations for Woocommerce when using Redis Cache

Open nikgoy opened this issue 11 years ago • 7 comments

I am using EasyEngine single site with Woocommerce. So, we will have to use some additional config. I created a additional file called woo.conf and placed it in /var/www/mysite.com/config/nginx as this directory is automatically included. The settings are

if ( $cookie_woocommerce_items_in_cart = "1" ){
     set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/shop.*|/cart.*|/my-account.*|/checkout.*|/addons.*|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $skip_cache 1;
}   

location ~ \.php$ {
    set $rt_session "";

    if ($http_cookie ~* "wp_woocommerce_session_[^=]*=([^%]+)%7C") {
                    set $rt_session wp_woocommerce_session_$1;
        }   

    if ($skip_cache = 0 ) {
        more_clear_headers "Set-Cookie*";
        set $rt_session "";
    }

    fastcgi_cache_key "$scheme$request_method$host$request_uri$rt_session";

    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass php;

    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;

    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid  60m;
}

I'm not sure about the location block. Though this seems to work fine, but essentially what should be the correct config settings here.

nikgoy avatar Sep 21 '15 06:09 nikgoy

@nikhilgoyal Above seem to be based on my config posted here - https://gist.github.com/rahul286/dc64ae84c97868b862c4

I never got time to test config correctly. Correct config will:

  1. Cache store pages for non-logged in visitors, as long as they are "just looking"
  2. As soon as a visitor adds something in cart, that and all subsequent request should skip cache for that visitor
  3. But cache should work for other visitors at the same time, who haven't added first item in cart

An easy way to test above is to shutdown php/mysql after cache warmup and see if product page is served from cache.

Can you please confirm if above is working fine?

rahul286 avatar Sep 21 '15 09:09 rahul286

Ok, as soon as I stop php-fpm , I get 502 - Bad Gateway error.

Plus I wanted to know more about this part, as I am using redis cache

    fastcgi_cache_key "$scheme$request_method$host$request_uri$rt_session";

    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass php;

    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;

    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid  60m;

Should I even have this part because after adding this.. the website has become quite slow. I am not even sure if the pages are being cached or not.

nikgoy avatar Sep 21 '15 13:09 nikgoy

I'm doing something very similar, but in my woo.conf file this is all I have, and it seems to work fine:

if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
         set $skip_cache 1;
}

cobbman avatar Sep 21 '15 17:09 cobbman

@bigwilliam The only problem with that setup is any dynamic part of the site (e.g. cart widget showing items in cart) will show a cached version. @rahul286 makes a good point earlier that once they have an item in their cart any pages looked at shouldn't be cached.

irhumbled avatar Oct 07 '15 03:10 irhumbled

This seems to work best for us regarding a mini-cart which is served on every page:

#Best practice so far if ($http_cookie ~* "cookie_woocommerce_items_in_cart"){ set $skip_cache 1; }

#Shouldn’t be needed (Above is faster) #if ( $cookie_woocommerce_items_in_cart = "1" ){ #set $skip_cache 1; #}

alexkappel avatar Nov 04 '15 09:11 alexkappel

Hey there. I've been following a few EE tutorials about optimizing woocommerce. I've came across two different code blocks regarding the setup.

I was wondering what's the difference between the two code blocks mentioned below, and more important yet, which one should I use? At the moment of writing, my website uses WooCommerce v. 2.6.4.

I created my website with EE version 3.7 by firing up ee create example.com --wpfc command. The website is live (not production optimized) yet, but it loads fast.

I'm not sure though the caching is working properly because I get a 404 GET response from Yith Wishlist plugin (I think). For details please see the support topic I created on rtCommunity forum.

Those are the different code blocks I mentioned, both described on EasyEngine tutorials section

WooCommerce Window Shopping Caching Technique

    if ($http_cookie ~* "wc_session_cookie_[^=]*=([^%]+)%7C") {
                set $rt_session wc_session_cookie_$1;
        }

WooCommerce Window Shopping Caching Technique

    if ($http_cookie ~* "wp_woocommerce_session_[^=]*=([^%]+)%7C") {
                    set $rt_session wp_woocommerce_session_$1;
        }   

I've tried both code blocks, and although cache seems to be working, I'm still getting the 404 GET response error.

I'd really appreciate if anyone could guide me on making woocommerce with fastcagi cache a truly optimized experience for the shop users.

Thanks in advance.

monecchi avatar Sep 09 '16 23:09 monecchi

We are cleaning up issue tracker and closing this issue because we won't be able to contribute to this discussion further.

Please use http://community.rtcamp.com/c/easyengine for future support questions.

rahul286 avatar May 29 '18 08:05 rahul286