Sites reporting fastcgi_cache BYPASS


#1

How do I find out why some of the sites I host almost always say: BYPASS?

i.e. this one: http://sustainableafrica.co.za/

#move next 3 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites  
        fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;  
        fastcgi_cache_key "$scheme$request_method$host$request_uri";  
        fastcgi_cache_use_stale error timeout invalid_header http_500;  
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
       location / {  
        try_files $uri $uri/ /index.html /index.php?$args;  
        }  
        set $skip_cache 0;  

        if ($request_method = POST) { set $skip_cache 1; }  
        if ($query_string != "") { set $skip_cache 1; }  

        if ( $arg_add-to-cart != "" ) { set $skip_cache 1; }  

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

        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|piwik_auth") { set $skip_cache 1; }  

        if ($request_uri ~* ("/wp-admin.*|/cart.*|/checkout.*|/account.*|/myaccount.*|/addond.*|/store.*|/shop.*|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/$  

        location ~ /purge(/.*) {  
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";  
        }
       location @php {  
            try_files $uri =404;  
            include /etc/nginx/fastcgi_params;  
            fastcgi_pass unix:/var/lib/php5-fpm/web45.sock;  
            fastcgi_index index.php;  
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
            fastcgi_intercept_errors on;  
            fastcgi_buffer_size 128k;  
            fastcgi_buffers 256 16k;  
            fastcgi_busy_buffers_size 256k;  
            fastcgi_temp_file_write_size 256k;  
            fastcgi_read_timeout 3000;  
            fastcgi_cache_bypass $skip_cache;  
            fastcgi_no_cache $skip_cache;  
            fastcgi_cache WORDPRESS;  
            fastcgi_cache_valid  60m;  
        }

Any ideas? What else could I check?


Ignore query parameters with fastcgi cache
#2

Just disable the following lines in your config file and restart nginx if ( $arg_add-to-cart != "" ) { set $skip_cache 1; }

Lets clean the nginx-cache rm -rf /var/run/nginx-cache/*

Restart nginx nginx -t && service nginx restart


#3

Didn't help. Any other ideas? I know that line was kind of redundant, seeing that I also do not cache queries:

if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; }


#4

Just playing with your server and found the issue

curl -sI -b woocommerce_items_in_cart=0 http://sustainableafrica.co.za/ | grep Fastcgi-Cache Fastcgi-Cache: HIT

Now we just need to disable the lines in nginx config and test

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


#5

Awesome! It works now. Any idea why that problem ocurred? That site doesn't even have Woocommerce on it :-/

This apparently broke fastcgi_cache for all my sites since I added that line globally...


#6

In your nginx config you told nginx don't cache the page if cookie_woocommerce_items_in_cart is not zero.

As the site don't have a the Woocommerce so the cookie_woocommerce_items_in_cart is not set that means cookie_woocommerce_items_in_cart is not equal to zero so don't cache the page.


#7

Ah, I understand. I didn't realize that if the variable isn't present at all, of course its value isn't 0. I'll skip using that check, except for woocommerce sites.

last question:

Can I also skip this one: if ( $arg_add-to-cart != "" ) { set $skip_cache 1; }

If these two are already used? if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; }


#8

And following the same logic, this would also break sites where woocommerce isn't installed, right? I understand it looks if arg_add-to-cart isn't empty but if it doesn't exist it also stops caching?

`#Skip cache for WooCommerce query string

if ($arg_add-to-cart != "") { set $skip_cache 1; }`


#9

Been running tests the whoel morning and I must say this: `

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

breaks caching completely even if woocomerce is installed.

And this: # Don't cache uris containing the following segments if ($request_uri ~* ("/wp-admin.*|/cart.*|/checkout.*|/account.*|/myaccount.*|/my-account.*|/addond.*|/addons.*|/store.*|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $skip_cache 1; }

Does not prevent my cart page: http://www.ict-consult.co.za/cart/ from being cached. Please advise!


#10

Hi guys, can you please have another look at this thread? As I said, even after deactivating all these problematic woocommerce related snippets, my cart was still being cached despite trying to exclude it like this:

if ($request_uri ~* ("/wp-admin.*|/cart.*|/checkout.*|/account.*|/myaccount.*|/my-account.*|/addond.*|/addons.*|/store.*|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $skip_cache 1; }


#11

Hello ovidiu,

Sorry for the late reply, if your site don't have a woocommerce installed then you don't needed the following rules

if ( $arg_add-to-cart != "" ) { set $skip_cache 1; }  

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

For woocommerce installed site enable above two rules also check this https://rtcamp.com/wordpress-nginx/tutorials/plugins/woocommerce/


#12

I have consolidated all the rules I could find on your site o into one not realizing some might break if woocommerce no present. what exactly does the cookie do part in the woocommerce link you gave me? would it work without it too?

at the moment I have:

`#activating fastcgi_cache 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; }

Skip cache for WooCommerce query string

if ($arg_add-to-cart != "") { set $skip_cache 1; }

below broke caching completely even if used on sites with woocommerce installed

Skip cache when WooCommerce cart is not empty

if ($cookie_woocommerce_items_in_cart != "0") { 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|piwik_auth") { set $skip_cache 1; }

Don't cache uris containing the following segments

if ($request_uri ~* ("/cart.*")) { set $skip_cache 1; } `

and the cache is always being bypassed.

NEXT, I comment this line:

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

and the cache starts working fine BUT it also caches my cart page even though above I have this line: if ($request_uri ~* ("/cart.*")) { set $skip_cache 1; }

Now that is a real problem. Can you confirm that this should/does actually work? => if ($request_uri ~* ("/cart.*")) { set $skip_cache 1; }


#13

If your site don’t have a woocommerce installed then you can try following rules which cache the page and /cart is always bypass

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

    # Don't Cache Uris Containing The Following Segments  
    if ($request_uri ~* "(/cart.*|/wp-admin/|/xmlrpc.php|wp-.*.php|index.php|/feed/|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;  
    }  

#14

Mitesh! Thank you so much! This config finally works and does not cache the cart! Did you find an error in my config above? I still cannot see the difference between yours and mine though.

Anyway, I will keep your last config as a tempalte for non-e-commerce sites.

If I run woocommerce, what exactly do I need to add? What exactly does the part of the instructions under "FastCGI Cache Issue" do where it says: set $rt_session ""; ?


#15

I'm drop the following codes in my config (copied from easyengine sample config file)

if ($arg_add-to-cart != “”) { set $skip_cache 1; }  
f ($cookie_woocommerce_items_in_cart != “0″) { set $skip_cache 1; }  

Also merge the following cart code

if ($request_uri ~* (“/cart.*”)) { set $skip_cache 1; } `  

into

if ($request_uri ~* "(/cart.*|/wp-admin/|/xmlrpc.php|wp-.*.php|index.php|/feed/|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {  
        set $skip_cache 1;  
    }  

For woocommerce sites you need to follow this article https://rtcamp.com/wordpress-nginx/tutorials/plugins/woocommerce/

Anyway, I will keep your last config as a tempalte for non-e-commerce sites You can try EasyEngine to setup server and manage wordpress sites easily.


#16

Thanks Mitesh! All good so far for non-e-commerce sites but I have to ask you again, what exactly does this part of the tutorial do and is it absolutely necessary?

http://screencast.com/t/vw4Crh8UMI


#17

@ovidiu

http://screencast.com/t/vw4Crh8UMI

That part clears woocommerce cookies for areas that should remain cached. It has complicated logic but works mostly.


#18