caddy -version)?v2.0.0-beta9 h1:oILdAOfunJ4ijBN9kOWjFIeH8EufBX/N1pC9HbnwjzU
Trying to match paths that should be forwarded to a reverse proxy
example.be
reverse_proxy /graphql localhost:4000
matcher reactPaths {
not {
path ^/graphql
}
}
try_files match:reactPaths {path}/ /index.html
file_server
caddy run
When I GET /graphql the request is forwarded to the reverse proxy, so that's good, but the URI is not as it's always /index.html, while I expect to end up localhost:4000/graphql
I always end up at localhost:4000/index.html
It seems that the proxy directive is used but I can't seem to exclude paths from the try_files
I can't think of anything.
Hi @eelkeh thank you for trying v2 while it is still in beta!
The try_files directive doesn't take matcher tokens: https://github.com/caddyserver/caddy/wiki/v2:-Documentation#try_files - because it is itself a matcher (combined with a rewrite).
In other words, this doesn't work they way you think:
try_files match:reactPaths {path}/ /index.html
Because it doesn't take matcher tokens, it interprets match:reactPaths as a file to try.
So you probably want this:
try_files {path}/ /index.html
Note that try_files is shorthand for:
matcher:try_files {
file {
try_files <files...>
}
}
rewrite match:try_files {http.matchers.file.relative}{http.request.uri.query_string}
So if you want to augment its matching logic with your own, you should probably just use the long form. The short form is just for convenience in the most common cases.
Also note that the path matcher does not accept regular expressions (for speed) but does accept certain wildcard patterns: ^/graphql won't work, but /graphql is the same thing.
If you really need to use a regexp (you don't, but FYI), then use the path_regexp matcher: https://github.com/caddyserver/caddy/wiki/v2:-Documentation#matcher
Hope this helps!
@mholt I'm running into a related issue. So I understand, that the first argument of try_files does not take a matcher.
Nevertheless, in the original post, it was asked to negate the /graphql path, which is not really possible with try_files, right?
The documentation also states that try_files is a shortcut for
@try_files {
file {
try_files <files...>
}
}
rewrite @try_files {http.matchers.file.relative}
Nevertheless, when I have try_files {path} {path}/ /index.html, I see different behaviour to:
@try_files {
file {
try_files {path} {path}/ /index.html
}
}
rewrite @try_files {http.matchers.file.relative}
With the shortcut, I see the expected behaviour, but with the long form, nothing is happening...
My goal in the end is to extend the matcher with a
not {
path /api/*
}
block though.
@fsonntag Sounds like you have a question! Please try asking on our forum. :) https://caddy.community - please be sure to post your full, unredacted (unedited) configuration along with a specific HTTP request and the exact response you're getting, along with what you expect. Then someone will be able to help you.
Will do, thanks!
@fsonntag I'm not able to replicate your issue with the short vs expanded try_files config. The outputs are identical.
As @mholt said, feel free to open an thread on https://caddy.community if you need any further help! I just wanted to verify this because it sounded strange to me.
Caddyfile:
:80
try_files {path} {path}/ /index.html
Output:
$ ./caddy adapt --config Caddyfile --pretty | sed 's/\t/ /g'
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"match": [
{
"file": {
"try_files": [
"{http.request.uri.path}",
"{http.request.uri.path}/",
"/index.html"
]
}
}
],
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
]
}
]
}
}
}
}
}
Caddyfile:
:80
@try_files {
file {
try_files {path} {path}/ /index.html
}
}
rewrite @try_files {http.matchers.file.relative}
Output:
$ ./caddy adapt --config Caddyfile --pretty | sed 's/\t/ /g'
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"match": [
{
"file": {
"try_files": [
"{http.request.uri.path}",
"{http.request.uri.path}/",
"/index.html"
]
}
}
],
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
]
}
]
}
}
}
}
}