Prioritize Proxy Over Rewrite

Is there a way that I can tell my Caddyfile to prioritize proxies over a rewrite?

Here is my Caddyfile:

aaronellington.com {
    proxy /test/ https://plex.ansible.one/

    root /var/www/are-web/web
    gzip

    rewrite {
        to {path} {path}/ /app.php?{query}
    }

    fastcgi / unix:/var/run/php/php7.0-fpm.sock php {
        index app.php
    }
}

It all works except for the proxy. I am getting a 404 page from app.php (my rewrite). Is there anyway to tell rewrite to ignore proxy paths or can I tell proxy to override all else?

I did find a fix for this but I do not like it. I can add this to my rewrite directive:

        if {path} not /test/

This is a simple test to try to figure out this problem but on my real server I have many proxies that I want to setup. I don’t like the idea of having to add 8+ if statements to my rewrite.

I know I’m kinda being petty about this but I just figured I would ask. Any suggestions?

You can try specify another vhost/location for the path you want to prioritise. Caddy will always use the configuration of the longest matching vhost. It would look like:

# Everything but the proxy
aaronellington.com {
    ...
}
# Proxies below:
aaronellington.com/test {
    proxy / https://plex.ansible.one
}
aaronellington.com/foo {
    proxy / https://some.backend/
}
aaronellington.com/bar {
    proxy / https://another.backend/
}
1 Like

That worked like a charm! Thank you!

I did not know that the vhost could contain more than a hostname and port. Awesome! I should have looked at the documentation more closly, haha!

2 Likes

Glad that worked!

For other readers, there’s a pretty great list of examples of what you can use as a vhost/location over at the Caddyfile documentation: https://caddyserver.com/docs/http-caddyfile#addresses

It’s not obvious – I’ll try to improve the docs for the new site. :slight_smile:

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