Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EasyEngine WooCommerce config with FastCGI Cache
#
set $skip_cache 0;
# POST requests and URL 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 URL containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|wp-.*.php|index.php|/feed/|sitemap$
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenter
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_n$
set $skip_cache 1;
}
# Skip cache on WooCommerce pages
if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
set $skip_cache 1;
}
# Skip cache for WooCommerce query string
if ( $arg_add-to-cart != "" ) {
set $skip_cache 1;
}
# Skip cache when WooCommerce cart is not empty
if ( $cookie_woocommerce_items_in_cart != "0" ) {
set $skip_cache 1;
}
# Use cached or actual file if they exists, Otherwise pass request to WordPress
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ ^/wp-content/cache/minify/(.+\.(css|js))$ {
try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$$
}
location ~ \.php$ {
set $rt_session "";
if ($http_cookie ~* "wc_session_cookie_[^=]*=([^%]+)%7C") {
set $rt_session wc_session_cookie_$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;
}
location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
@pelmered
Copy link
Author

pelmered commented Mar 27, 2015

Instuctions

Save this file to /etc/nginx/common/wpfcwc.conf
Adjust the WooCommerce URLs on line 25 to match the URLs in your store(the URLs here match the default URLs for an English install).

Generate the site as usual with --wpfc

Then edit /etc/nginx/sites-enabeld/yourdomain as following.
Change:
include common/wpfc.conf;
Into:
include common/wpfcwc.conf;

@sydios
Copy link

sydios commented Jul 15, 2015

Hi when i use this config i always get an error when i test it with nginx -t
There is no "c" in line 20 and i also commentet this line out but it still the same issue.

thanks

nginx: [emerg] unexpected "c" in /etc/nginx/common/wpfcwc.conf:20
nginx: configuration file /etc/nginx/nginx.conf test failed

This my wpfcwc.conf

set $skip_cache 0;

POST requests and URL 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 URL containing the following segments

if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|wp-..php|index.php|/feed/|/chef-dashboard.|/user-profile.|/newsletter.|sitemap$
set $skip_cache 1;
}

Don't use the cache for logged in users or recent commenter

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

Skip cache on WooCommerce pages

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

Skip cache for WooCommerce query string

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

Skip cache when WooCommerce cart is not empty

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

Use cached or actual file if they exists, Otherwise pass request to WordPress

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ ^/wp-content/cache/minify/(.+.(css|js))$ {
try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$$
}

location ~ .php$ {

    set $rt_session "";

    if ($http_cookie ~* "wc_session_cookie_[^=]*=([^%]+)%7C") {
            set $rt_session wc_session_cookie_$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;

}

location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}

@rajtatata
Copy link

rajtatata commented Sep 26, 2016

For anyone who can't get the fastcgi cache to work...

I noticed in my website that when cart is empty , the headers don't have a set cookie for woocommerce_items_in_cart.
It is not woocommerce_items_in_cart = 0 , it just doesn't exist.
If this is the case for you , change the code :

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

to:

#true only when the cookie exists
if ( $cookie_woocommerce_items_in_cart ) {
set $skip_cache 1;
}

For me when I add to cart and then make cart empty again , the cookie just disappears, so you can just go ahead and only check for it if it exists.

@x0n0x
Copy link

x0n0x commented Jul 13, 2017

Some lines of code are missing that causing an error, here is what I found:

• On the line 20:

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_n$

should be:

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {

• The line 45:

try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$$

should be:

try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 /index.php;

@djeraseit
Copy link

djeraseit commented Aug 14, 2017

You forgot one...

  • On line 15

if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|wp-.*.php|index.php|/feed/|sitemap$

should be:

if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment