文档
一个 项目

handle_path

其工作方式与 handle 指令 相同,但会隐式使用 uri strip_prefix 来剥离匹配到的路径前缀。

处理匹配特定路径的请求(同时从请求 URI 中剥离该路径)是一个足够常见的用例,因此为方便起见提供了专门的指令。

语法

handle_path <path_matcher> {
	<directives...>
}
  • <directives...> 是一系列 HTTP 处理指令或指令块,每行一个,就像在 handle_path 块外使用时一样。

只接受且必须提供单个 路径匹配器;不能在 handle_path 中使用命名匹配器。

示例

以下配置:

handle_path /prefix/* {
	...
}

👆 实际上与下面的这个等效 👇,但 handle_path 形式 👆 更简洁一些

handle /prefix/* {
	uri strip_prefix /prefix
	...
}

下面是一个完整的 Caddyfile 示例,其中 handle_pathhandle 互斥;但请注意 子文件夹问题

example.com {
	# Serve your API, stripping the /api prefix
	handle_path /api/* {
		reverse_proxy localhost:9000
	}

	# Serve your static site
	handle {
		root * /srv
		file_server
	}
}