AndreKR
(André Hänsel)
February 24, 2024, 1:45am
1
I’m reading Request matchers (Caddyfile) — Caddy Documentation and I wonder, if I want to match /api
and /api/items
but I do not want to match /apineapple
, do I have to write two lines like
@my-matcher {
path /api
path /api/*
}
or is there a shorter way?
1 Like
matt
(Matt Holt)
February 24, 2024, 3:24am
2
I think that’s the way to do it (without a regex) To me it’s very clear and makes sense.
Shorter one-line syntax:
@matcher path /api /api/*
I had suggested an alternate syntax some time ago, i.e. /api/*?
but Matt wasn’t a fan
caddyserver:master
← caddyserver:path-opt-slash-syntax
opened 08:48PM - 22 Aug 21 UTC
This is an idea taken from the WICG URLPattern draft, that I thought would be a … perfect fit for Caddy.
https://github.com/WICG/urlpattern/blob/main/mdn-drafts/QUICK-REFERENCE.md
It's reasonably common that users want to match both `/foo` and `/foo/*` at the same time, but not `/foobar`. This is relevant when some service is being proxied to under a subpath, but you want to make sure there's no "leakage" in the path matching, matching more than will actually function properly.
The URLPattern draft has the `/*?` syntax which means "make the wildcard and slash optional". This is a pretty neat pattern that can fold `/foo` and `/foo/*` as above into just `/foo/*?` to match the exact same thing in one rule.
This means it can also be used for inline path matchers to do both those things at once, usually saving a line of Caddyfile config.
Before:
```
example.com
@service path /service /service/*
reverse_proxy @service localhost:8000
```
After:
```
example.com
reverse_proxy /service/*? localhost:8000
```
1 Like
matt
(Matt Holt)
February 24, 2024, 6:37pm
4
Oh yeah, forgot the one-line thing.
I still don’t like the question mark
1 Like
AndreKR
(André Hänsel)
February 26, 2024, 10:10pm
5
I have to say, I don’t like the question mark either, because my brain reads it like a regex where *
is not special, so it becomes “/api/
or /api/something
” which is not what it’s supposed to mean (that would be “/api
or /api/something
”).
So yeah, the one-liner is fine for me.
By the way, I had trouble finding in the docs whether
@my-matcher {
path a b
}
is equivalent to
@my-matcher {
path a
path b
}
(The answer is yes.)
It does say
Multiple paths will be OR’ed together.
but that could very well apply only to multiple given paths after a single path
keyword.
1 Like
matt
(Matt Holt)
February 26, 2024, 10:54pm
6
Good q, they get combined, so yeah they are identical.
system
(system)
Closed
March 27, 2024, 10:54pm
7
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.