Proxy: Path Match

I want to use caddy as reverse proxy since it’s incredibly simple especially on backends with tls.

I want to use caddy to handle external traffic. I want to implement something like facebook does.

Users going to path /:profile gets routed to their profile page. same goes with /:page, /:group and so on… and also there’s some declared url paths such as /register, /login etc…

If you want users to be routed to /:profile or /:group you use something like
... proxy /<path> upstream... { except /login /register } ... to exclude the declared url paths.

how do I proxy users if they try to access /:profiles and not be routed to /:groups? how can I tell caddy that this url is a profile and this is not etc etc..?

I implemented something like ask redis for all profilenames and groupnames, update caddyfile’s proxy to add the names. I’m not sure if itll impact caddy’s performance. Most like it will.

... proxy /<path> upstream... { except /login /register /username1 /username2 /group1 /group2 ... /usernameX /groupX } ...

Any ideas on this? thanks in advance!

Hi,

I’d move all usersnames, groups etc. to path with prefix: /username1 → /user/username1

Then proxy only the known static paths with something like this:

proxy /user …
proxy /group …

and you don’t need to mess up config with excepts.

@Vladimir_Dudr thanks for the feedback man. That was my temporary solution before. I was trying to find a solution (if possible) for caddy to distinguish which is which. I want the users to just access mydomain.com/<profile> to see a profile and mydomain.com/<group> for groups.

for now I have ... rewrite { if {path} not login if {path} not register to /profile/{path} /group/{path} /notfound }

then I have services listening on /profile/<profilename> and /group/<groupname> so I proxy them
proxy /profile <profile-upstreams>... {} proxy /group <group-upstreams>...{}

but It seems everything is going to /notfound. valid profile and group still all routes to /notfound. if I try to remove /notfound then all requests get routed to /group/<groupname>. I dunno why.

still couldn’t figure out a way :confused:

I guess it won’t work with rewrites as documentation says that Caddy rewrites to first existing path in list of destinations. Last in the list acts as default. So you’ll need more rewite rules to do what I guess you’re trying to do.

You are trying to mix application dynamic content with webserver configuration which is wrong in my opinion. Also what would you do if you have group and profile of the same name? If you insist on having that short URLs it is application itself what should handle this I think. So I suggest you to just pass all the path as recieved and build a router in the application.

2 Likes

I agree. This is out of scope.

thanks again. yeah I agree I should handle this in application level. I was just curious if caddy can handle this sort easily. Anyway thanks again! :+1:

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