大网数据广告活动
服务器运维问答

Which matcher does Caddy pick when there is a conflict?

· 52CE 我爱测

Directives in an Caddyfile can have "matchers" that limit them to specific requests. This lets Caddy 2 serve different content for different paths, but what happens when multiple directives match the same request? If I want to se…

问题原文

原帖链接 · William Swanson · CC BY-SA 4.0

Directives in an Caddyfile can have "matchers" that limit them to specific requests. This lets Caddy 2 serve different content for different paths, but what happens when multiple directives match the same request?

If I want to serve a mostly-static website using Caddy 2, but forward an /api/ area to some Node.js process, my Caddyfile might look like this:

fake-example.edge.app {
  root * /var/www/example
  reverse_proxy /api/* localhost:9000
  file_server *
}

A request to /api/user would match both the file_server * and reverse_proxy /api/* matchers, so it's not clear whether the request will go to the filesystem or the Node.js process.

I couldn't find anything in the Caddy 2 documentation that describes what should happen. In my own testing, the results seem to depend on the order the directives appear in the file, with earlier entries "winning". What is supposed to happen? If it's "undefined behavior", is there a better way to write this file to avoid the ambiguity?

已采纳解答

原帖链接 · pic · CC BY-SA 4.0

In my understanding and experience reverse_proxy takes precedence because directives have a default implicit order: https://caddyserver.com/docs/caddyfile/directives#directive-order .

To force a different order you have at least 3 options:

  1. use a route:
    route /api/* {
      file_server
      reverse_proxy localhost:9000
    }
  1. change the default order with the order global option
  2. use handle with file_server before reverse_proxy

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-06-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。