问题描述
我试图让 Caddy 服务器显示一个动态文件(分配给一个变量)。我发现官方文档有点不透明,搜索互联网也没有返回太多信息。
我的示例 Caddyfile
配置:
example.com {
route / {
# own plugin that adds custom headers to the request
my_plugin do_something
map {header.something} {my_file} {
x "x.html"
y "y.html"
default "404.html"
}
file_server {
browse {my_file}
}
}
}
所以这个想法是如果请求包含值为“y”的“东西”,则 Caddy 显示“y.html”模板。你得到了图片。
然而,Caddy 会抱怨:
http.log.error parsing browse template: parsing browse template file: open {my_file}: no such file or directory
看起来它在字面上需要“{my_file}”(!)。
如何在 Caddy 中完成这项工作?
注意
其他示例,例如 redir {my_file}
工作并重定向到“example.com/y.html”。
templates {my_file}
不起作用。
解决方法
好的,我想通了。最终。
简单但不那么明显的语法:
*.example.com {
map {labels.2} {my_file} {
x "x.html"
default "404.html"
}
root * tmpl
file_server
templates
try_files {my_file}
}
因此,如果域是 x.example.com
,它将呈现位于当前工作目录的 x.html
文件夹中的 tmpl
模板。 templates
指令仅在模板包含任何函数/操作(例如包含)时才需要。