Woocommerce and fastcgi_cache BYPASS


#1

Hi, I have followed the steps in your guide, about Woocommerce and Nginx. I wan’t caching on, but as soon the visitor puts something in the basket, the entire site should not be cached anymore, or else there is alot of trouble with WooCommerce.

I have tryed this code:

if ( $cookie_woocommerce_items_in_cart != “0” ) { set $no_cache 1; }

But when it’s active, the site is never cached, for anyone, and the status is bypassed.

Is there something I can do, or is it impossible to achive, what I want? :slight_smile:


#2

You can try code snippet here - https://gist.github.com/rahul286/dc64ae84c97868b862c4


#3

my current working example.com conf

server {


    server_name gooeypixel.com   www.gooeypixel.com;
    
        #Yoast sitemap
location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
        rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
        rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
        rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

        ## following lines are options. Needed for wordpress-seo addons
        rewrite ^/news_sitemap\.xml$ /index.php?sitemap=wpseo_news last;
        rewrite ^/locations\.kml$ /index.php?sitemap=wpseo_local_kml last;
        rewrite ^/geo_sitemap\.xml$ /index.php?sitemap=wpseo_local last;
        rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;

    access_log off;
}


    access_log /var/log/nginx/gooeypixel.com.access.log rt_cache; 
    error_log /var/log/nginx/gooeypixel.com.error.log;


    root /var/www/gooeypixel.com/htdocs;
    
    

    index index.php index.html index.htm;


    include common/wpfc.conf;      
    include common/wpcommon.conf;
    include common/locations.conf;
    include /var/www/gooeypixel.com/conf/nginx/*.conf;
    
}

used the bypass snippet from @rahul286

server {

server_name gooeypixel.com www.gooeypixel.com;

access_log   /var/log/nginx/gooeypixel.com.access.log rt_cache;
error_log    /var/log/nginx/gooeypixel.com.error.log debug;

root /var/www/gooeypixel.com/htdocs;
index index.php index.htm index.html;

fastcgi_cache_use_stale error timeout invalid_header http_500;

set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $skip_cache 1;
}   
if ($query_string != "") {
    set $skip_cache 1;
}   

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;
}   

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}
location / {
    try_files $uri $uri/ /index.php?$args;
}   



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;
}


include /etc/nginx/common/wpcommon.conf;
include /etc/nginx/common/locations.conf;

} 

restarted nginx

nginx -t && service nginx reload

Nginx reloads and gives and ’ ok ’ but now my example.com site will not load, i revised the file back so my working example.com site is now working but still having issues with the woo cart not caching correctly. Any help greatly appreciated. Thanks in advanced.


#4

Ok so after looking at the file more in depth i forgot to add the following code.

include /var/www/gooeypixel.com/conf/nginx/*.conf;

My example.com site is now connecting, I will continue to test out my cart to make sure the appropriate pages are caching correctly…


#5