Multiple matchers for the same block in Caddyfile

1. Caddy Version

v2.7.6

2. How I run Caddy:

Simply with caddy run on a Windows server environment.

The issue

This is just a usage issue, and probably a dumb one on my side.

I can’t understand how to have multiple matchers for a single block.
In addition, it seems to me that the php_fastcgi directive is not capturing, in the sense that if there’s a handle block that also matches, it will be applied even if successive to the php_fastcgi block.

Thus I ended having a very weird and verbose Caddyfile:

:80 {
    root * wwwroot
    
    php_fastcgi / {
        to localhost:9000
        to localhost:9001
        to localhost:9002
        to localhost:9003
        to localhost:9004
        to localhost:9005
        to localhost:9006
        to localhost:9007
    }

    php_fastcgi /index.php {
        to localhost:9000
        to localhost:9001
        to localhost:9002
        to localhost:9003
        to localhost:9004
        to localhost:9005
        to localhost:9006
        to localhost:9007
    }
	
    php_fastcgi /src/functions.pages.php {
        to localhost:9000
        to localhost:9001
        to localhost:9002
        to localhost:9003
        to localhost:9004
        to localhost:9005
        to localhost:9006
        to localhost:9007
    }
    
	php_fastcgi /sinottici/sm/ {
        to localhost:9000
        to localhost:9001
        to localhost:9002
        to localhost:9003
        to localhost:9004
        to localhost:9005
        to localhost:9006
        to localhost:9007
    }
	
    php_fastcgi /sinottici/sm/index.php {
        to localhost:9000
        to localhost:9001
        to localhost:9002
        to localhost:9003
        to localhost:9004
        to localhost:9005
        to localhost:9006
        to localhost:9007
    }
	
	handle /src/classes/**/*.php {
		respond "403 Forbidden"
	}
	handle /src/ajax.php {
		respond "403 Forbidden"
	}
    handle /src/config.php {
		respond "403 Forbidden"
	}
    handle /src/defines.php {
		respond "403 Forbidden"
	}
    handle /src/functions.php {
		respond "403 Forbidden"
	}
    handle /src/install.php {
		respond "403 Forbidden"
	}
    handle /src/utils.php {
		respond "403 Forbidden"
	}
    handle /templates/**/utils.php {
		respond "403 Forbidden"
	}
	handle /sinottici/sm/src/pages/*.php {
		respond "403 Forbidden"
	}
	
	file_server

	log {
		output file logs\access.log
	}
}

I’m pretty sure this is not correct, but I could not find the correct way to do all of the above despite scouring the docs for a couple hours.

First of all, I think your project’s files are not laid out correctly. You should have a separate webroot directory like public/ that your index.php and any other scripts that should be directly hit should live in. Any files that should not be directly hit live outside of public (in directories sibling to public).

For matching, please read the docs: Request matchers (Caddyfile) — Caddy Documentation

If you need to match more than a single path, then you need to use a named matcher.

For responding with an error, use the error directive (not respond) which triggers and error that you can then handle with handle_errors (to display a custom error page or whatever).

You can apply a single named matcher to a single error, and another named matcher to a single php_fastcgi.

But ideally, your config should just look like this if your project is properly laid out with index.php acting as the entrypoint, inside of public/.

Unfortunately I have no control over my project files, as it’s a very ancient (and abismally engineered) application from my workplace. I am very aware that it’s laid out in a very weird way.

I got around using named matchers instead, which made things a little cleaner.

Thanks for your tip on error too.

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