perf: don't use a placeholder as root in the default Caddyfile and add docs

This commit is contained in:
Kévin Dunglas 2024-11-12 12:13:18 +01:00
parent 102b4d1ad0
commit 44c6ead4f1
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
3 changed files with 36 additions and 2 deletions

View File

@ -19,7 +19,7 @@
# }
#}
root * public/
root public/
encode zstd br gzip
# Uncomment the following lines to enable Mercure and Vulcain modules
@ -41,5 +41,7 @@
{$CADDY_SERVER_EXTRA_DIRECTIVES}
php_server
php_server {
root {$PWD}/public/
}
}

View File

@ -52,6 +52,37 @@ set the `GOMEMLIMIT` environment variable to the available amount of memory.
For more details, [the Go documentation page dedicated to this subject](https://pkg.go.dev/runtime#hdr-Environment_Variables) is a must-read to get the most out of the runtime.
## Explicitly Set The Root Directory
> [!TIP]
>
> The default `Caddyfile` provided in the Docker images already contains this optimization.
When not explicitly set, FrankenPHP sets the `root` option of the `php_server` directive to the `{http.vars.root}` placeholder.
The value of this placeholder [can be configured using the `root` directive provided by Caddy](https://caddyserver.com/docs/caddyfile/directives/root)
and can contain dynamic values.
While convenient, this feature comes at a cost in terms of performance because the root directory is resolved at runtime,
and, depending on your configuration, may change with each request.
Explicitly set the root directory in the `php_server` directive to avoid this cost:
```caddyfile
php_server {
root /path/to/your/public/dir/
}
```
Alternatively, you can use [the `PWD` environment variable](https://pubs.opengroup.org/onlinepubs/009604599/utilities/cd.html),
which always contain the path to the current directory on POSIX systems (including on Linux, macOS and FreeBSD).
This variable will only be resolved once, at Caddy startup:
```caddyfile
php_server {
root {$PWD}/public/
}
```
## `file_server`
By default, the `php_server` directive automatically sets up a file server to

View File

@ -8,6 +8,7 @@ http:// {
encode zstd br gzip
php_server {
root {$PWD}
file_server off
resolve_root_symlink false
}