handle_path
運作方式與 handle
指令 相同,但會隱含使用 uri strip_prefix
來移除比對到的路徑字首。
處理比對到特定路徑的請求(同時從請求 URI 中移除該路徑)是很常見的用例,因此有專屬指令可簡化處理。
語法
handle_path <path_matcher> {
<directives...>
}
- <指令...> 是 HTTP 處理常式或指令區塊清單,每列一個,就像在
handle_path
區塊外部使用的一樣。
只接受單一 路徑比對器,且為必填;無法將命名比對器與 handle_path
搭配使用。
範例
此設定
handle_path /prefix/* {
...
}
👆 實際上與以下 👇 相同,但 👆 的 handle_path
形式略為簡潔
handle /prefix/* {
uri strip_prefix /prefix
...
}
完整的 Caddyfile 範例,其中 handle_path
和 handle
互斥;但請注意 子資料夾問題
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
}
}