Php_fastcgi exclude path from try_files

1. The problem I’m having:

I can’t get php_fastcgi to exclude paths from try_files

2. Error messages and/or full log output:

Error during parsing: parsing caddyfile tokens for ‘php_fastcgi’: /etc/caddy/Caddyfile:31 - Error during parsing: unrecognized response matcher not

	php_fastcgi unix//var/run/php/php-fpm.sock {
		@try_files {
			not path /var/*
			try_files {path} {path}/index.php index.php
		}
	}

3. Caddy version:

2.7.0-beta.1 h1:hKYXjAR/7Tn/DVfsu9j1ER8O1qLHh6163a7RoStRBXI=

4. How I installed and ran Caddy:

Docker compose

a. System environment:

Docker

d. My complete Caddy config:

{
	# Debug
	{$CADDY_DEBUG}
}

{$SERVER_NAME} 

{$CADDY_EXTRA_CONFIG}

log

route {
	root * /srv/app/public
	mercure {
		# Transport to use (default to Bolt)
		transport_url {$MERCURE_TRANSPORT_URL:bolt:///data/mercure.db}
		# Publisher JWT key
		publisher_jwt {env.MERCURE_PUBLISHER_JWT_KEY} {env.MERCURE_PUBLISHER_JWT_ALG}
		# Subscriber JWT key
		subscriber_jwt {env.MERCURE_SUBSCRIBER_JWT_KEY} {env.MERCURE_SUBSCRIBER_JWT_ALG}
		# Allow anonymous subscribers (double-check that it's what you want)
		anonymous
		# Enable the subscription API (double-check that it's what you want)
		subscriptions
		# Extra directives
		{$MERCURE_EXTRA_DIRECTIVES}
	}
	vulcain
	php_fastcgi unix//var/run/php/php-fpm.sock {
		@try_files {
			not path /var/*
			try_files {path} {path}/index.php index.php
		}
	}
	encode zstd gzip
	file_server
}

Went about it the wrong way I guess. Instead of excluding try file just rewrite to index.php

	rewrite /var/* index.php
1 Like

Did you intend to define a response matcher or a request matcher?

@try_files {
	not path /var/*
	try_files {path} {path}/index.php index.php
}
php_fastcgi @try_files unix//var/run/php/php-fpm.sock

I don’t understand what you’re trying to do. php_fastcgi will rewrite to index.php if the file doesn’t exist.

Are you saying that you have requests to /var/* that map to files that do exist on disk and you want it to hit PHP for those instead of serving them?

I’d suggest that those files should probably not be in /srv/app/public then, it should be somewhere outside of your webroot instead. You should only have files you actually want to serve inside your webroot.

Correct I want them to hit indexe.pho

I understand that being in public is odd but that is were the platform stores images stored by the cms, and I am adding access control to the images via a custom controller and at the moment moving the storage location is not an option but agreed is the better solution

I tried this variation and I get

Error: adapting config using caddyfile: parsing caddyfile tokens for 'route': getting matcher module 'try_files': module not registered: http.matchers.try_files

Oops, wrote in a hurry:

@try_files {
	not path /var/*
	file {path} {path}/index.php index.php
}
php_fastcgi @try_files unix//var/run/php/php-fpm.sock

Honestly, I think this is the most correct option here instead of trying to muck with try_files.

1 Like

I like this too, but I will try the above also as I am new to caddy and figuring how matchers and directive mix.

1 Like

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