Running php-app in a subdirectory causes problems

1. Caddy version (caddy version):

caddy v2.4.6

2. How I run Caddy:

a. System environment:


b. Command:

docker-compose caddy up

d. My complete Caddyfile or JSON config:

mydomain {
    handle_path /app/* {
        root * /srv/public

        #rewrite /app/* /{path}
        php_fastcgi php:9000 {
            #env REQUEST_URI /

    handle {
        reverse_proxy service:8080

3. The problem I’m having:

I’m trying to inject a Symfony-application (PHP) into an existing service. Everything to /app should go to symfony, elsewise to the service. The reverse-proxy to the service runs great, but I’m struggling to get the setup to symfony right. With the config shown, the request reaches symfony, but it contains /app in the uri, so the error-message from symfony is No route found for "GET https://mydomain/app/". Thats right - symfony is expecting to serve the application in /, not in /app.

5. What I already tried:

You can see my attempts commented out. When using env REQUEST_URI / the request is answered correclty from symfony - but that does only work for / - nothing else (like /app/test123). Additionally, as symfony is not aware of that rewrite, it generates every link wrong: Symfony generates /img/logo.png for example, but the right path would be /app/img/logo.png. So the response coming from symfony/fpm must be changed somehow.

Symfony uses only the REQUEST_URI-header for routing, so changing somewhere in PATH_INFO does not help.
It would be possible to change routing in symfony (like $_SERVER['REQUEST_URI']=str_replace('/app/','/',$_SERVER['REQUEST_URI']);), but thats not good behaviour, since the included app should not be aware of being reverse proxied.

It seems, that it works for everybody, except me, so I might have a missunderstand somewhere - or there is some other way to accomplish the goal.
I’m aware, that its possible to solve the problem with a sub-domain - however, in this specific case thats not an option.

Help is greatly appreciated :slight_smile:

6. Links to relevant resources:

other issue, that goes into the direction of my problem:

I think Symfony should have a base path config you can use to ensure it ignores the leading /app during routing.

This isn’t an easy problem to resolve, because there’s all kinds of moving pieces under the hood in Caddy to make sure the path is properly manipulated. The php_fastcgi directive is a shortcut for a bunch of different pieces of logic, which you can read about here: php_fastcgi (Caddyfile directive) — Caddy Documentation

Thanks for the fast answer!

There is no base-path-config in symfony - there is only an option to set uri-defaults when its running without a request (in cli for example): Routing (Symfony Docs)

I’m aware that php_fastcgi is a shortcut, already tried in that direction, but without success.

Can you use route prefixes?

Thats only a patch for controllers - that does not solve path-resolution for assets like css, javascripts or images.

And again, I believe, that the target-application should not be aware of the proxying. Or is that wrong?

It’s hard for any application to be served from a subpath, really. Here’s an post that explains:

In this case, the fact that Caddy is modular makes this difficult to resolve. If one module makes a decision (like a rewrite, or uri strip_prefix as handle_path implicitly performs) means that modules need to inspect the request state to try to figure out what to do.

The REQUEST_URI variable by definition is supposed to be “the original request URI”, so we send that as-is, no modifications.

You can override REQUEST_URI yourself with the env option, but I’m not sure that will always have the right effect. You could try though.

1 Like

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