Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

404 on Preview – WordPress & Varnish

I’ve recently moved a number of properties to a server running Apache 2.4, PHP FPM 5.4, MariaDB 10. & Varnish 4.0. The gain in performance is phenomenal. Varnish does wonders to speed up your property.

One issue reported was that when someone clicked the Preview button while creating a post, they’d be shown a 404 error message.

Turns out the fix is pretty straight forward, you have to tell varnish to let any request with the tag preview=true through to the backend and not serve a cached page to it.

To apply the fix, open your vcl at /etc/varnish (CentOS & Ubuntu) and add the following code to the vcl_recv part

sub vcl_recv {
        .
        .
        .
        # Allow posts preview 
        if (req.url ~ "preview=true") { 
                return(pass); 
        }
        .
        .
        .
}

Note: This is only part of the configuration required to get WordPress to run with Varnish.