Caddy: v2: reverse_proxy with try_files

Created on 14 Nov 2019  路  5Comments  路  Source: caddyserver/caddy

1. Which version of Caddy are you using (caddy -version)?

v2.0.0-beta9 h1:oILdAOfunJ4ijBN9kOWjFIeH8EufBX/N1pC9HbnwjzU

2. What are you trying to do?

Trying to match paths that should be forwarded to a reverse proxy

3. What is your Caddyfile?

example.be

reverse_proxy /graphql localhost:4000
matcher reactPaths {
    not {
        path ^/graphql
    }
}
try_files match:reactPaths {path}/ /index.html
file_server


4. How did you run Caddy (give the full command and describe the execution environment)?


caddy run

5. Please paste any relevant HTTP request(s) here.

6. What did you expect to see?


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

7. What did you see instead (give full error messages and/or log)?


I always end up at localhost:4000/index.html

8. Why is this a bug, and how do you think this should be fixed?


It seems that the proxy directive is used but I can't seem to exclude paths from the try_files

9. What are you doing to work around the problem in the meantime?


I can't think of anything.

10. Please link to any related issues, pull requests, and/or discussion.

Bonus: What do you use Caddy for? Why did you choose Caddy?

question

All 5 comments

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}"
                }
              ]
            }
          ]
        }
      }
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mschneider82 picture mschneider82  路  3Comments

billop picture billop  路  3Comments

aeroxy picture aeroxy  路  3Comments

whs picture whs  路  3Comments

klaasel picture klaasel  路  3Comments