1. Caddy version (caddy version
):
v2.2.1 h1:Q62GWHMtztnvyRU+KPOpw6fNfeCD3SkwH7SfT1Tgt2c=
2. How I run Caddy:
systemd on Ubuntu Linux in production; though right now I’m running caddy run
on a Mac laptop for testing.
a. System environment:
Ubuntu Linux, eventually; OSX right now.
b. Command:
./caddy run
d. My complete Caddyfile or JSON config:
127.0.0.1:80 {
@missingFile not file {path} {path}.md
handle @missingFile {
respond @missingFile "Not found" 404
}
@notes {
path_regexp \/\d{4}\/?.*$
}
handle @notes {
templates
rewrite * /note.html
}
root * /Users/smerrill/skippy/content/
templates
file_server
rewrite /* /index.html
}
3. The problem I’m having:
I don’t completely understand how named matches, routes, and handlers interact.
What I want is to serve different templates for different paths. Request for /foo
should use the index.html
template, and requests for /2020/10/30/foo
should use the note.html
template.
The old v1 Markdown system was simple and worked extremely well for my needs. I’m finding it hard to implement all the logic in my Caddyfile to make this work for v2.
4. Error messages and/or full log output:
2020/11/01 00:35:33.972 ERROR http.log.error template: /index.html:8:20: executing "/index.html" at <include $markdownFilePath>: error calling include: open /Users/smerrill/skippy/content/2020/10/30/foo.md: no such file or directory {"request": {"remote_addr": "127.0.0.1:64903", "proto": "HTTP/1.1", "method": "GET", "host": "127.0.0.1", "uri": "/2020/10/30/foo", "headers": {"Connection": ["keep-alive"], "Upgrade-Insecure-Requests": ["1"], "Dnt": ["1"], "Sec-Gpc": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate"], "Accept-Language": ["en-US,en;q=0.5"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0"]}}, "duration": 0.00057551, "status": 500, "err_id": "9wxq8khf6", "err_trace": "templates.(*Templates).executeTemplate (templates.go:315)"}
2020/11/01 00:35:34.019 ERROR http.log.error template: /index.html:8:20: executing "/index.html" at <include $markdownFilePath>: error calling include: open /Users/smerrill/skippy/content/favicon.ico.md: no such file or directory {"request": {"remote_addr": "127.0.0.1:64904", "proto": "HTTP/1.1", "method": "GET", "host": "127.0.0.1", "uri": "/favicon.ico", "headers": {"User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0"], "Dnt": ["1"], "Sec-Gpc": ["1"], "Accept": ["image/webp,*/*"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate"], "Connection": ["keep-alive"], "Referer": ["http://127.0.0.1/2020/10/30/foo"]}}, "duration": 0.000710707, "status": 500, "err_id": "c4z2e8yzb", "err_trace": "templates.(*Templates).executeTemplate (templates.go:315)"}
5. What I already tried:
As you can see from the Caddyfile, I’m trying to set up different templates for different paths, and also trying to properly handle 404 errors.
I have two major paths in my site: /YYYY/MM/DD/XYZ
for short-form notes, and then long-form posts off the root. I would like different templates for each of these.
When I visit http://127.0.01/2020/10/30/foo
, I expect to get a 404 error because there is no such file. Instead, Caddy is trying to use the /index.html
template. Even if the 404 fails, I would expect the /note.html
template to be used.
Here is my index.html
template, which is a basic effort to output a Markdown file:
<html>
<body>
{{$pathParts := splitList "/" .OriginalReq.URL.Path}}
<br />
{{$markdownFilename := default "index" (slice $pathParts 1 | join "/")}}
{{ $markdownFilename }}
{{$markdownFilePath := printf "/%s.md" $markdownFilename}}
{{$markdownFile := (include $markdownFilePath | splitFrontMatter)}}
{{$title := default $markdownFilename $markdownFile.Meta.title}}
{{ $title }}
</body>
</html>
And here is note.html
which is intended to be used for the request I’m trying, and also a minimal debug effort:
<html>
<head><title>note</title></head>
<body>
{{$pathParts := splitList "/" .OriginalReq.URL.Path}}
<br />
{{$markdownFilename := default "index" (slice $pathParts 1 | join "/")}}
{{ $markdownFilename }}
{{$markdownFilePath := printf "/%s.md" $markdownFilename}}
{{$markdownFile := (include $markdownFilePath | splitFrontMatter)}}
{{$title := default $markdownFilename $markdownFile.Meta.title}}
{{ $title }}
</body>
</html>
The error message I posted above indicates that index.html
is being used for the request.
6. Links to relevant resources:
V2 Markdown, Templates, and HTTP error codes - my original post in my effort to move from Caddy v1 to v2.