-
-
Notifications
You must be signed in to change notification settings - Fork 160
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
Examples to add to the docs #324
Comments
@mholt happy to help. working / maintained examples are really needed. How about making a list of types of examples that you feel are appropriate, so then people can co-ordinate around that ? |
Basically, just post your Caddy configs you're already using and we'll take it from there. It would be nice if you comment parts that aren't obvious, or which are optional, that could be helpful. |
I have a request for a config example. My general reverse proxy config looks like this:
I used to have a directive in there to serve a simple |
@karimfromjordan you're looking for https://caddyserver.com/docs/caddyfile/directives/handle_errors. The |
We make heavy use of snippets and ENV's for our TLS on-demand setup
|
This is what I have running at the moment. I have 2 caddy servers to achieve high availability. Their only function is to get SSL certificates and do reverse proxying. Initial config present on both Caddy servers:
Config that is centrally hosted and gets loaded through HTTP:
|
Related: https://github.com/search?q=%28path%3A**%2FCaddyfile+OR+path%3A**%2F*.caddy%29&type=code Would be nice to scrape all of these (~7k apparently, probably a fraction of that after dedepulicating), then put a simple but blazing fast search on top of them (could even be client side only). The use case: often you have an idea or problem you need to solve, and the fastest way to validate that and see how it's possible is to search a corpus of config others wrote. Also, often the best way to understand a directive or config parameter is to see the different ways it's used in the real world. (tangent: I tried to add the |
feels like we need a high level categorisation from easy to hard to classify the config examples. and then a examples repo where the examples are exercised. The endpoints just need to respond with fake data - so just a simple main.go for each endpoint. Also would suggest using overmind to run each example. Its just a procFile for each example folder. In the end, this would make it so much easier to use caddy and have someone to go to see when things screw up in your config. |
I would suggest example.com {
handle_path /foo {
reverse_proxy localhost:1234
}
} as an example for consolidating microservices under domains or similar. |
@mustafa0x Thanks for that search, I dunno how useful a pile of 7K of them will be, but maybe I can scan those and look for some common patterns that stick out to me! @gedw99 Evaluating examples is a whole 'nother dealio; maybe we can have a Caddy sandbox/playground, but the new site won't initially have one because of priorities. @randomairborne Thanks! I will have to make a note with that example that most backend apps don't like being proxied to in a "subfolder" unless they're explicitly configured to do so. |
I'm happy to see this issue is getting some love. It's one thing having helpful users and developers in the community site, but standalone resources and examples would have been brilliant for me. I have a mix of elegant and horrible examples… so you can have one of each, for now. CORSThis is based on Setup CORS in Caddy 2 and Caddy Server, CORS, and Preflight Requests - AUXNET but changes (fixes?) a few things.
I'm not 💯 it's doing all the right things; check for your own use case. (cors) {
@cors_preflight{args.0} {
method OPTIONS
header Origin {args.0}
}
@cors{args.0} header Origin {args.0}
handle @cors_preflight{args.0} {
header {
Access-Control-Allow-Origin "{args.0}"
Access-Control-Allow-Credentials true
Access-Control-Allow-Headers "Authorization, Cache-Control, Content-Type"
Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE"
Access-Control-Max-Age "3600"
Vary Origin
defer
}
respond "" 204
}
handle @cors{args.0} {
header {
Access-Control-Allow-Origin "{args.0}"
Access-Control-Allow-Credentials true
Access-Control-Expose-Headers "{args.1}"
Vary Origin
defer
}
}
} Use as follows: import cors https://{$HOST} "ETag" Rewriting query parametersThis is a bit of a bruiser… and I think there's a race condition, as the iteration ordering of go maps is not defined (but is usually alphabetical…) — if I can reproduce it, I'll raise an issue. This snippet pulls out a query parameter that is set to true, and builds a list of (query_select) {
@query_{args.0} query {args.0}=true
vars @query_{args.0} {
select "{vars.select}{vars.sep}{args.0}"
sep ","
}
} Use as follows. https://{$HOST} {
import query_select first
import query_select second
rewrite * /?new_query={vars.select}
…
} This then converts |
@mholt this one is really specific to things like APIs that don't directly interact with the user, imo |
@dbaynard Thanks, there's some good content there. @randomairborne Which one, exactly? |
@mholt #324 (comment) is specifically for a lot of API servers, and will utterly break a lot of more traditional templated websites. |
Yes, true -- as I said above, most websites don't like that kind of proxying, so we'd have to make a note of it. I like the distinction between templated sites and "API servers". Makes a lot of sense. |
I found and fixed an issue, with this (the issue is in the sources I referenced). When imported multiple times, only the origin from the first was allowed. Now only the pre-flight with the correct origin matches.
|
How are you passing the ENV to Caddy ( I definitely like the approach with environment variables, but |
We could probably change that. I don't think it would be able to reset the environment but we could at least update any prior/existing values and add new ones. |
Hi Matt, I think that would be very helpful to have I'm still starting with Caddy Server configuration and still doing lots of tests and trials regarding my configuration. I have to adopt a lot of logic and features from Caucho Resin to Caddy. An ability to have Caddy reload the Caddyfile configuration with consideration of values from envfile would be great. |
Getting off topic here, but I don't see how Regarding the difference between I'll try to clean up the explanation on https://caddyserver.com/docs/caddyfile/concepts#environment-variables but it's essentially saying the same thing. |
The second paragraph is an awesome explanation. That would be very beneficial for the documentation. |
At a meta level, I'm happy to see you (maintainers) going through this process. I came to caddy from tailscale, and there are many things I like about it, but it has been difficult to figure out how to use its functionality, in particular for what must be common use cases. So I'm happy to help contribute, too. |
I've hit a situation that the CORS block above doesn't handle: if the Origin should be a regex. There's a straightforward modification to handle this situation, and I've created a variable to have the actual matched origin. However, in doing so, I found bugs in my configuration that took me a long time to resolve. As part of it, I learned how to interpret the actual caddy json structure. Turns out I had to wrap the I did know about Similar directives — handle (Caddyfile directive) — Caddy Documentation but didn't clock that the reason caddy was returning a The differences between (I'm not particularly obsessed with CORS configuration, it's just that's where my attention is needed…) |
We are planning to add a large repository of examples to the docs.
However, the breadth of use cases is nearly infinite and there are only a few of us maintainers, so we need your help by posting your configs for your use cases here.
Please post your examples here. Doesn't matter how niche; let us decide that. If you think it's useful, or something you wish you had when you were starting, share it!
We are accepting both JSON and Caddyfile configurations. Note that we may revise examples when posting them to our site. Feel free to reply here with your examples to contribute.
Instructions
I would love to see 100+ examples posted here from all of you.
Thank you!
Here's an example 😉
The text was updated successfully, but these errors were encountered: