Caddy + file_server 的动态文件名

问题描述

我试图让 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 指令仅在模板包含任何函数/操作(例如包含)时才需要。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...