Excluding folder from fastcgi

I’ve got a bunch of WordPress sites on Caddy, and I’m trying to ensure anything under wp-content doesn’t get passed to fastcgi.

I’ve managed to get this working for a specific file, like this:

fastcgi / /run/php/php7.0-fpm.sock php {
    except /wp-content/uploads/2018/05/test.php
}

However, what I’d really like to do is:

fastcgi / /run/php/php7.0-fpm.sock php {
    except /wp-content/uploads/*
}

Is there a way to do that? My various attempts at that haven’t worked thus far.

In case it’s relevant, here’s my full Caddyfile:

www.example.com {
    header / Strict-Transport-Security "max-age=31536000"
    log /var/log/caddy/example.access.log
    errors /var/log/caddy/example.error.log
    gzip

    root /var/www/example.com

    fastcgi / /run/php/php7.0-fpm.sock php {
        except /wp-content/uploads/2018/05/test.php
    }

    rewrite {
        if {path} not_match ^\/wp-admin
        to {path} {path}/ /index.php?_url={uri}
    }
}

Looking at the fastcgi code, it seems like the excepted paths are checked exactly, which the docs imply but don’t outright state.

One neat workaround would be to define another site block in your Caddyfile so requests for /wp-content never make it to fastcgi at all. It will require you to copy over the other relevant configuration. You could share them between the two with a snippet.

(shared-config) {
    header / Strict-Transport-Security "max-age=31536000"
    log /var/log/caddy/example.access.log
    errors /var/log/caddy/example.error.log
    gzip
}

www.example.com/wp-content {
    import shared-config
    root /var/www/example.com/wp-content
}

www.example.com {
    import shared-config
    root /var/www/example.com

    fastcgi / /run/php/php7.0-fpm.sock php {
        except /wp-content/uploads/2018/05/test.php
    }

    rewrite {
        if {path} not_match ^\/wp-admin
        to {path} {path}/ /index.php?_url={uri}
    }
}

We should probably make it match prefixes, rather than exact paths… what do you think, @abiosoft?

Thanks @matt and @Whitestrake. I’ve got this working for now - I had to change the root of the wp-content site block - I think paths are relative to the prefix that defines the block (so it needs root to be /var/www/example.com/wp-content instead of /var/www/example.com), but that’s definitely good enough for my needs.

In future, having the except directive match prefixes rather than exact paths makes sense to me though, so if that’s doable that’d be awesome :slight_smile:.

1 Like

You’re right! I missed that. I’ll update my example in case anyone else comes here looking for this problem.

I’ve logged this as an issue

https://github.com/mholt/caddy/issues/2160

This should actually be labelled a bug, I thought I made it match prefix. :see_no_evil:.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.