Skip to content
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

docs: tryfiles directive #1

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default defineConfig({
{ link: "/templates", text: "templates" },
{ link: "/timeouts", text: "timeouts" },
{ link: "/tls", text: "tls" },
{ link: "/tryfiles", text: "tryfiles" },
{ link: "/websocket", text: "websocket" },
]
},
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ documented, but will be in due course.
- ❓ Provide multi-platform Docker images at `ghcr.io/tmpim/casket`
- Allow use of multiple headers for loadbalancing policy in the [`proxy`](/proxy) directive
([#1](https://github.com/tmpim/casket/pull/1))
- Add `try_files` directive from Caddy v2 ([#15](https://github.com/tmpim/casket/pull/15))
- Add [`tryfiles`](/tryfiles) directive, similar to Caddy v2 ([#15](https://github.com/tmpim/casket/pull/15))
- Add `exclude` option to the [`basicauth`](/basicauth) directive
([52c171f](https://github.com/tmpim/casket/commit/52c171f6c6d5941e0fd3e75aaad202a68f1305bc))
- Add `servearchive` option to the [`browse`](/browse) directive to download folders as archives
Expand Down
61 changes: 61 additions & 0 deletions docs/tryfiles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# http.tryfiles

<script setup>
import NewInCasket from "./components/NewInCasket.vue";
</script>

<NewInCasket />

tryfiles is a directive that allows you to specify a list of files to try to serve from the filesystem defined by the
[root](/root) directive. If none of the files exist, the request is passed on to the next handler in the chain, such as
a [proxy](/proxy) handler.

This directive makes it easier to serve compiled static content (React, Vue, etc.) for single page applications. The
default configuration will first try to serve the requested file, then try to serve an index page (e.g. `index.html`) if
the requested file does not exist.

## Syntax

``` casketfile
# Uses a sensible default configuration, works for most SPAs (see below)
tryfiles
```

This default configuration is equivalent to:

``` casketfile
tryfiles {path} index.html index.htm index.txt default.html default.htm default.txt {
except /.well-known {cfg.addr.path}/.well-known
without {cfg.addr.path}
}
```

This default configuration even works if the site block is defined on a subpath, e.g. `example.com/subpath`.

You can customize the list of files to try:

``` casketfile
tryfiles [paths...] {
except paths...
without prefix
}
```

- **paths** is a list of space-separated file paths to try in order. [Placeholders](#placeholders) are supported.
- **except** is a list of space-separated file paths to exclude from the list of files to try.
[Placeholders](#placeholders) are supported.
- **without** is a path prefix to strip from the request path before trying to find the files on the filesystem.

## Examples

Serve `index.html` if the requested file does not exist:

``` casketfile
tryfiles
```

Try the requested file, then `index.php` with the query string appended:

``` casketfile
tryfiles {path} /index.php?{query}&p={path}
```