Caddy with Symfony put under a symlinked subdirectory

Continuing the discussion from Symfony app under symlinked dir, subpath handled by file_server instead of php_fastcgi:

I can say now that even though I still have the symlink for browsing purposes, the handle directive is better to set a custom root just for this handle block.

However, there is still an issue.

I updated Caddy to v2.6.2, and I also updated the Caddyfile’s vhost:

demo.orbitale.io {
        encode gzip

        handle / {
                root / /var/www/demo.orbitale.io/www/
                file_server browse
        }

        # The app we're looking for
        handle /orbitale_cms/* {
                uri strip_prefix /orbitale_cms
                root * /var/www/demo.orbitale.io/repos/demos.orbitale_cms/public/
                php_fastcgi unix//run/php/php8.1-fpm.sock {
                        root /var/www/demo.orbitale.io/repos/demos.orbitale_cms/public/
                }
        }

        # Fallback
        php_fastcgi * unix//run/php/php8.1-fpm.sock

        file_server

        tls <redacted>
        log {
                output file /var/www/demo.orbitale.io/access.log {
                        roll_size 512mb
                        roll_keep_for 720h
                }
        }
}

When I go to https://demo.orbitale.io/orbitale_cms/admin, an URL that can trigger Symfony’s backend, and it shows me a 404 not found: cannot find url /orbitale_cms/admin, so the Symfony backend is actually called.

If I just replace the whole index.php with solely phpinfo();, I see that the document root and script name are okay, but the REQUEST_URI still contains the wrong part even with the uri strip_prefix /orbitale_cms directive in the Caddyfile.

Am I forgetting something about this prefix? I found nothing relevant in the php_fastcgi expanded form :confused:

Edit: I even tried to copy/paste the expanded form and add the header_up X-Forwarded-Prefix /orbitale_cms directive, to see if it wasn’t a reverse-proxy issue or something, and it doesn’t do anything.

I managed to make it work with several workarounds.

First, added some params to the app itself, as shown in this diff : Comparing da3fe4b21ae05c5e005e06d735c474c8b8ea47ef...b2ee649a1c207706a24b7b58e34a8d1037f92595 · Orbitale/demos.orbitale_cms · GitHub

Also, I used the expanded form of the php_fastcgi directive and added the header_up X-Forwarded-Prefix /orbitale_cms , to enforce Symfony understanding this prefix is important.

From what I know, it works , but it seems really hacky just for a subdirectory :confused:

By design, REQUEST_URI is the original URI from the request, unmodified by rewrites.

The problem is that you have two rewrites happening here. The one from uri strip_prefix, then the one from the built-in try_files in php_fastcgi, which rewrites it to /index.php.

One thing you could do is capture the {path} at the point right after the prefix is stripped and then overwrite REQUEST_URI with that value.

	handle_path /orbitale_cms/* {
		vars subpath_uri {uri}
		root * /var/www/demo.orbitale.io/repos/demos.orbitale_cms/public
		php_fastcgi unix//run/php/php8.1-fpm.sock {
			env REQUEST_URI {vars.subpath_uri}
		}
	}

Here’s we’re using handle_path instead of handle + uri strip_prefix because it saves a line, plus ensures the order of operations is correct (because otherwise the directive order would mess with it).

Keep in mind that if you do this, then your PHP app will have no way of knowing the original URI, so it won’t know how to perform redirects or how to construct paths that reach the same handle block. You’ll need to configure your backend app somehow to be aware of a base URL.

The explanation from this article applies here as well:

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