-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v2: Caddyfile enhancements #2959
Comments
Caddyfile-generated subroutes have handlers, which are sorted first by directive order (this is unchanged), but within directives we now sort by specificity of path matcher in descending order (longest path first, assuming that longest path is most specific). This only applies if there is only one matcher set, and the path matcher in that set has only one path in it. Path matchers with two or more paths are not sorted like this; and routes with more than one matcher set are not sorted like this either, since specificity is difficult or impossible to infer correctly. This is a special case, but definitely a very common one, as a lot of routing decisions are based on paths.
Items 1, 2, and 7 are finished, 4 and 5 are more like notes/clarifications, and 8 is in progress... so that really leaves 3 and 6 as the major TODOs yet. For 3, I'm thinking instead of a table format under a single
Something like that. |
Will this work also include allowing the Caddyfile format to support directives in https://caddyserver.com/docs/json/apps/tls/automation/policies/management/acme/ and https://caddyserver.com/docs/json/logging/? |
@smebberson Not specifically this issue, but the Caddyfile upgrades as a whole, that are planned before the release candidates, yes |
I did actually discover https://github.com/caddyserver/caddy/tree/v2#how-do-i-avoid-lets-encrypt-rate-limits-with-caddy-2 just recently (I haven't tried it yet though), so maybe more of that capability exists than is written in the docs at the moment. |
This applies only to rewrites in the top-level subroute created by the HTTP caddyfile.
I decided to leave rewrites be mutually exclusive, without a single table as in #2959 (comment). These upgrades are done! (Logging, error handling, TLS stuff will all come later.) Please try it out as soon as you can, before the next beta release, which will be in a few days I think. |
* http: path matcher: exact match by default; substring matches (#2959) This is a breaking change. * caddyfile: Change "matcher" directive to "@matcher" syntax (#2959) * cmd: Assume caddyfile adapter for config files named Caddyfile * Sub-sort handlers by path matcher length (#2959) Caddyfile-generated subroutes have handlers, which are sorted first by directive order (this is unchanged), but within directives we now sort by specificity of path matcher in descending order (longest path first, assuming that longest path is most specific). This only applies if there is only one matcher set, and the path matcher in that set has only one path in it. Path matchers with two or more paths are not sorted like this; and routes with more than one matcher set are not sorted like this either, since specificity is difficult or impossible to infer correctly. This is a special case, but definitely a very common one, as a lot of routing decisions are based on paths. * caddyfile: New 'route' directive for appearance-order handling (#2959) * caddyfile: Make rewrite directives mutually exclusive (#2959) This applies only to rewrites in the top-level subroute created by the HTTP caddyfile.
@mholt would you be able to provide an example of a reverse_proxy + stripping the prefix on a particular route. I have not quite groked how that will work.
|
Directive name always comes first. Within a route, directives are ordered by appearance. FYI, there might be some more changes coming up, after a discussion with a user tonight with a common use case that I don't think we've covered yet. |
The Caddyfile is one of the last things to be worked on in v2 since it is just a thin layer over Caddy 2's config, in fact, it is a separate plugin (whereas in v1, it was the central mode of configuration). What we have at this point was just brought over from v1 with very few changes -- most of the changes are totally internal (like the fact that it just generates JSON now). The matchers are the only real external change. A good one, but clunky.
Anyway, it's time to get it up to speed, and get it documented, so people will actually want to use Caddy again.
There are a number of deficiencies in the Caddyfile from v1 and in v2 currently that have been studied over the last few months (and years, frankly), which we now have a chance to improve on. If I have time I will try to link to them here, but if you've done anything mildly advanced with the Caddyfile, you know what I'm talking about.
I don't want to sacrifice the Caddyfile's simplicity and usability, though, either.
Since we're still in beta, now is the time to make breaking changes to get it right. Also, since the current Caddyfile documentation is scant, I suspect very few people will be impacted by the changes.
These solutions/enhancements could go a lot of different ways, but here's what I've settled on as of now:
*
.Example: Proxying all API endpoints, before:
And after:
Before, this example would be impossible, since the first line would always match those in the second line, and would never evaluate the second line since proxying is terminal:
After this change, the above will be valid, since it will be sorted in the other order: first matching handler wins.
rewrite
directive will be mutually exclusive -- only the first matching rewrite will be executed. It will not "rehandle" -- this means rewrites do not cascade.Example: Our new v2 documentation site uses something like this:
But this ends up redundant because the first rewrite makes it into the second rewrite, since rewrites "rehandle". With the change, only the first rewrite would be evaluated for requests to /docs/json/*.
rehandle
directive which is exactly the same as rewrite but allows cascading. I have not yet found a situation where rehandling in the Caddyfile is the only way to an elegant solution.Example:
strip_prefix
,strip_suffix
, anduri_replace
which change the request URI like rewrite does, but these directives are not mutually exclusive (with each other andrewrite
) because they do not signify the same intent asrewrite
. This took forever for me to figure out: therewrite
directive says "change the URI to this, as if the request came in like that" but the other directives say "manipulate the URI a bit before further processing". Therewrite
directive will still be able to do these things like stripping prefix/suffix and do replacements, but as subdirectives.Example: both of these will be evaluated:
route
directive which takes a matcher and then opens a block of directives. Any directives contained within it are evaluated in the order they are specified. I don't think new matchers can be defined in the block... in fact, only handler directives should be able to be used, I think; the whole block is treated as a single unit (but matchers can be used with each individual directive like normal) when composing handler routes. Theroute
directive will be evaluated after the middleware handlers but before the terminating handlers likefile_server
,reverse_proxy
, andphp_fastcgi
.For example, to solve this problem, we can reduce its solution to about 4 lines with an explicit route:
matcher name { ... }
andmatch:...
to@name
(or=name
, not sure yet) for both definition and usage.Example (somewhat contrived): Before:
After:
Feedback is welcomed, but I'm going to start working on these changes tomorrow.
The text was updated successfully, but these errors were encountered: