Caddy
Caddy is a modern, open-source web server written in Go. Its main draw is automatic HTTPS: Caddy obtains and renews TLS certificates for you with no extra configuration. Like Nginx, Caddy does not read Apache .htaccess files, so Grav ships an equivalent Caddyfile in the webserver-configs/ folder of every release.
Requirements
caddy(v2 or newer)php-fpm(FastCGI Process Manager) for PHP 8.3 or newer (Grav 2's minimum) to process PHP scripts
Quick start
Grav ships a ready-to-run Caddyfile in webserver-configs/. To try it locally, run the following from the root of your Grav site:
caddy run --config webserver-configs/Caddyfile
By default this serves the site at https://localhost and redirects http://localhost to it (Caddy v2 enables HTTPS automatically). Make sure your PHP-FPM process is listening on 127.0.0.1:9000, or adjust the php_fastcgi address to match your setup (for example php_fastcgi unix//run/php/php8.3-fpm.sock for the Ubuntu 24.04 socket).
The bundled Caddyfile
The shipped file is the canonical reference. For production, replace localhost with your domain so Caddy can provision a real certificate, and point root at your Grav directory:
1example.com
2encode gzip
3root * /var/www/grav
4
5# Begin - Security
6
7# deny all direct access for these folders
8@denied_dirs path_regexp (?i)^/(\.git|cache|bin|logs|backups?|tmp|webserver-configs|tests)/
9
10# deny all direct access to these sensitive user folders, whatever the file type
11@denied_user_config path_regexp (?i)^/user/(env/[^/]+/)?config/
12
13# block user/accounts too, but allow avatar images to be served directly, whether
14# stored at user/accounts/avatars/<file> (flatfile accounts) or user/accounts/
15# <username>/<file> (Flex folder storage). SVG is excluded as a stored-XSS vector.
16# (Go's RE2 has no lookbehind, so this uses a negated matcher.)
17@denied_user_accounts {
18 path_regexp (?i)^/user/(env/[^/]+/)?accounts/
19 not path_regexp (?i)^/user/(env/[^/]+/)?accounts/[^/]+/[^/]+\.(jpe?g|png|gif|webp|avif|bmp|ico)$
20}
21
22# block user/data too, but allow public media uploads (e.g. Flex Object images)
23# to be served directly. SVG is intentionally excluded as a stored-XSS vector.
24# (Go's RE2 has no lookbehind, so this uses a negated matcher.)
25@denied_user_data {
26 path_regexp (?i)^/user/(env/[^/]+/)?data/
27 not path_regexp (?i)\.(jpe?g|png|gif|webp|avif|bmp|ico|mp4|webm|ogg|ogv|mov|mp3|wav|m4a|flac|pdf)$
28}
29
30# Serve page media only through the page route, so plugins can apply the page's
31# `access` rules to it. DO NOT uncomment this matcher and its `respond` line
32# below without first setting `pages.media_route_urls: true` in
33# user/config/system.yaml, or every media URL on the site becomes a 403.
34# @denied_user_pages {
35# path_regexp (?i)^/user/pages/
36# }
37
38# deny running scripts in the public cache folders (image derivatives and combined assets)
39@denied_cache_scripts path_regexp (?i)^/(images|assets)/.*\.(php|php2|php3|php4|php5|php7|php8|phar|phtml|pht|phtm|phps|pl|py|cgi|sh|bat)$
40
41# deny running scripts inside core system folders
42@denied_system path_regexp (?i)^/(system|vendor)/.*\.(txt|xml|md|html|htm|shtml|shtm|json|yaml|yml|php|php2|php3|php4|php5|phar|phtml|pl|py|cgi|twig|sh|bat)$
43
44# deny running scripts inside user folder
45@denied_user_scripts path_regexp (?i)^/user/.*\.(txt|md|json|yaml|yml|php|php2|php3|php4|php5|phar|phtml|pl|py|cgi|twig|sh|bat)$
46
47# deny access to specific files in the root folder
48@denied_root_files path_regexp (?i)^/(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt)$
49
50# deny Markdown source files such as README.md and CHANGELOG.md; a page route
51# ending in .md is not a file, so it still reaches Grav's Markdown output
52@denied_markdown_files {
53 path_regexp (?i)\.md$
54 file
55}
56
57# deny hidden files and directories at any depth (.htaccess, .env, .env.local,
58# .migrating, nested .git repositories), except /.well-known/ files whose own
59# names don't start with a dot (ACME challenges, security.txt).
60# Caddy matchers use RE2, which has no lookahead, so .well-known is excluded
61# with a separate `not` rather than inline in the expression.
62@denied_dotfiles {
63 path_regexp (?i)(^|/)\.
64 not path_regexp ^/\.well-known(/[^./][^/]*)+$
65}
66## End - Security
67
68@immutable path_regexp (?i)/_app/immutable/
69
70route {
71 respond @denied_dirs 403
72 respond @denied_user_config 403
73 respond @denied_user_accounts 403
74 respond @denied_user_data 403
75 # respond @denied_user_pages 403
76 respond @denied_cache_scripts 403
77 respond @denied_system 403
78 respond @denied_user_scripts 403
79 respond @denied_root_files 403
80 respond @denied_markdown_files 403
81 respond @denied_dotfiles 403
82
83 # global rewrite should come last.
84 try_files {path} {path}/ /index.php?_url={uri}&{query}
85
86 # Content-hashed bundles (the Admin panel and other SvelteKit-based plugins)
87 # can be cached for a year: the name changes whenever the file does. After
88 # try_files on purpose, so a missing chunk has been rewritten to index.php
89 # by now and Grav's 404 doesn't match.
90 header @immutable Cache-Control "public, max-age=31536000, immutable"
91
92 php_fastcgi 127.0.0.1:9000
93 file_server
94}
Note
Caddy's path matcher is literal and only understands * wildcards, so every rule that needs alternation, character classes or anchors uses a named path_regexp matcher. Outside a route block Caddy applies its own directive order, and the global try_files rewrite would run before respond, so the rules sit in a route block, which runs top to bottom as written. Because Go's RE2 engine has no lookbehind or lookahead, the user/accounts, user/data and .well-known exceptions are negated matchers rather than single patterns. The Markdown rule pairs path_regexp with the file matcher, so only .md files that exist on disk are refused and page routes ending in .md still reach Grav's Markdown output. The config, accounts and data rules also match under an optional env/<name>/ prefix, so a multisite subsite's own folders are protected while its themes, plugins and assets stay public. The @immutable matcher gives content-hashed bundles under _app/immutable/ (the Admin panel and other SvelteKit-based plugins and themes) a one-year immutable cache; it sits after try_files on purpose, so a missing bundle file has already been rewritten to index.php and Grav's 404 is not cached. Caddy does not read .htaccess, so when you upgrade Grav, compare your Caddyfile with the shipped webserver-configs/Caddyfile and copy any changed rules by hand.
Running as a service
For a permanent install, place your edited file at /etc/caddy/Caddyfile and manage Caddy with systemd:
sudo systemctl reload caddy
Confirming it works
A request for a private file such as https://example.com/user/config/system.yaml should return 403 Forbidden, while a media file you uploaded under user/data (for example a .jpg) should still load normally. See the User Folder Exposure page for more on the admin warning that fires when these rules are not applied.