// Troubleshooting EDD + Nginx FastCGI Cache

We recently noticed some caching issues with Easy Digital Downloads when combined with Nginx’s built-in FastCGI Caching.

The problem

Users would occasionally see a “Checkout” button on the Buy page even though they hadn’t added anything to their cart.

It turns out that user sessions were being cached as well. We needed a way to bypass cache for users with items in their cart.

Fortunately for us, the Easy Digital Downloads team had already addressed this issue by setting an edd_items_in_cart cookie when the cart isn’t empty.

The solution

Here’s where we turn on caching, inside nginx.conf’s http { } section:

fastcgi_cache_path /PATH/TO/cache levels=1:2 keys_zone=FACETWP:100m inactive=4h;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

And these are the relevant server { } lines (notice how we’re using the edd_items_in_cart cookie):

set $no_cache 0;

if ($request_method = POST) {
    set $no_cache 1;
}
if ($query_string != "") {
    set $no_cache 1;
}
if ($request_uri ~* "(/wp-admin/|/wp-*.php|/buy/|/checkout*|/purchase-confirmation*)") {
    set $no_cache 1;
}
if ($http_cookie ~* "(wordpress_logged_in_*|edd_items_in_cart)") {
    set $no_cache 1;
}

# relevant params
location ~ \.php$ {
    fastcgi_no_cache $no_cache;
    fastcgi_cache FACETWP;
    fastcgi_cache_valid 200 4h;
}