結構
塊:
所有的指令必須在 { } 塊中,如果只有一個站點,則塊標記可以省略。
全局的配置塊可以放在最上方,其次是站點的配置塊。
指令:
指令是服務於站點配置的關鍵詞。
關鍵詞和引號:
空格在 Caddyfile 指令中很重要,如果希望帶空格的字符為一個值,需要給它們加上引號。
directive abc def
directive "abc def"
directive "\"abc def\""
directive `"foo bar"`
地址
地址位於塊的最上方。
有效的地址如下,
localhost
example.com
:443
http://example.com
localhost:8080
127.0.0.1
[::1]:2015
example.com/foo/*
*.example.com
http://
如果您的站點地址包含主機名或IP地址,則啟用自動HTTPS。
但是,此行為是純隱式的,因此它永遠不會覆蓋任何顯式配置。
例如,如果站點的地址為http://example.com,則不會激活自動HTTPS。
通過把地址寫在一起可以匹配多個站點,地址必須是唯一的。
localhost:8080, example.com, www.example.com {
}
或者
localhost:8080,
example.com, www.example.com
匹配器
請求匹配器用於對請求進行分類。
root * /var/www # matcher token: * root /index.html /var/www # matcher token: /index.html root @post /var/www # matcher token: @post
更多關於 Matcher。
占位符
左邊的簡記符等價於右邊的。
{dir} |
{http.request.uri.path.dir} |
{file} |
{http.request.uri.path.file} |
{header.*} |
{http.request.header.*} |
{host} |
{http.request.host} |
{labels.*} |
{http.request.host.labels.*} |
{hostport} |
{http.request.hostport} |
{port} |
{http.request.port} |
{method} |
{http.request.method} |
{path} |
{http.request.uri.path} |
{path.*} |
{http.request.uri.path.*} |
{query} |
{http.request.uri.query} |
{query.*} |
{http.request.uri.query.*} |
{re.*.*} |
{http.regexp.*.*} |
{remote} |
{http.request.remote} |
{remote_host} |
{http.request.remote.host} |
{remote_port} |
{http.request.remote.port} |
{scheme} |
{http.request.scheme} |
{uri} |
{http.request.uri} |
{tls_cipher} |
{http.request.tls.cipher_suite} |
{tls_version} |
{http.request.tls.version} |
{tls_client_fingerprint} |
{http.request.tls.client.fingerprint} |
{tls_client_issuer} |
{http.request.tls.client.issuer} |
{tls_client_serial} |
{http.request.tls.client.serial} |
{tls_client_subject} |
{http.request.tls.client.subject} |
代碼片段
代碼片段是特殊的塊,命名用擴展包起來。
(redirect) { @http { scheme http } redir @http https://{host}{uri} }
然后,可以在其它地方引入使用。
import redirect
注釋
注釋使用 # 號,必須在行首或者行尾。
環境變量
如果配置依賴於環境變量,可以使用如下方式
{$SITE_ADDRESS}
在運行前會被解析為正確的值。
如果希望在運行時解析,你可以使用標准的 {env.*} 占位符。
全局選項
全局選項是沒有 key 的塊。
{
...
}
如果全局選項存在,它必須是配置中的第一個塊。
它用於設置全局應用的選項,或不適用於特定的任何站點。在內部,只能設置全局選項。您不能在其中使用常規站點指令。
更多。