caddy2 在配置上采取了动态配置站点, 并且不兼容一代配置。

语法

caddy2file

备注

# 备注内容

占位符

ShorthandReplaces
{dir}{http.request.uri.path.dir}
{file}{http.request.uri.path.file}
{host}{http.request.host}
{hostport}{http.request.hostport}
{method}{http.request.method}
{path}{http.request.uri.path}
{query}{http.request.uri.query}
{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}

片段

# 默认跳转 https
(redirectHttps) {
	@http {
		scheme http
	}
	redir @http https://{host}{uri}
}

使用 import redirectHttps 来调用

指令

DirectiveDescription
basicauthEnforces HTTP Basic Authentication
bindCustomize the server's socket address
encodeEncodes (usually compresses) responses
file_serverServe files from disk
handleA mutually-exclusive group of directives
handle_errorsDefines routes for handling errors
headerSets or removes response headers
logEnables access/request logging
php_fastcgiServe PHP sites over FastCGI
redirIssues an HTTP redirect to the client
request_headerManipulates request headers
respondWrites a hard-coded response to the client
reverse_proxyA powerful and extensible reverse proxy
rewriteRewrites the request internally
rootSet the path to the site root
routeA group of directives treated literally as single unit
templatesExecute templates on the response
tlsCustomize TLS settings
try_filesRewrite that depends on file existence
uriManipulate the URI

指令语法

directive [<matcher>] <args...> {
	subdirective [<args...>]
}

使用时候有顺序要求

root

header

redir
rewrite

uri
try_files

basicauth
request_header
encode
templates

handle
route

respond
reverse_proxy
php_fastcgi
file_server

php

{
    debug
    admin off
}

(php) {
	# Add trailing slash for directory requests
	@canonicalPath {
		file {
			try_files {path}/index.php
		}
		not path */
	}
	redir @canonicalPath {path}/ 308

	# If the requested file does not exist, try index files
	@indexFiles {
		file {
			try_files {path} {path}/index.php index.php
			split_path .php
		}
	}
    try_files {path} {path}/index.php

	# Proxy PHP files to the FastCGI responder
	@phpFiles {
        path *.php
		path *.php/*
        path */*.php/*
	}
	reverse_proxy @phpFiles 127.0.0.1:9000 {
		transport fastcgi {
			split .php
		}
	}
}


:9097 {
    import php

    file_server browse    
}