Struggling with php_fastcgi and browsing a subdirectory

You’ll need to either use a route or handle block to solve this.

Basically, the problem is that the php_fastcgi directive is sorted above file_server, according to this pre-determined order.

You could either do this:

route {
    file_server /stuff/* browse
    php_fastcgi unix//run/php/php-fpm.sock
    file_server
}

Or this:

handle /stuff/* {
    file_server browse
}

handle {
    php_fastcgi unix//run/php/php-fpm.sock
    file_server
}

The route directive overrides the sorting, so you’re telling Caddy to always handle /stuff/* first. The handle directive instead makes each block mutually exclusive from eachother.

Both solutions are valid, but I would personally lean towards using handle because it’s more flexible if you need to make additional changes.

1 Like