参考资料

飞雪无情Caddy实战系列

Caddy实战(一)| 托管你的网站,只需一行命令

Caddy官方文档

Welcome - Caddy Documentation

Caddy中文文档

欢迎 - Caddy v2中文文档

下载安装

https://caddyserver.com/download 从官方下载页下载就可以, go编译的二进制文件

指令列表

https://www.zhe.ink/handbook/caddy2/docs/caddyfile/directives.html

匹配器

https://www.zhe.ink/handbook/caddy2/docs/caddyfile/matchers.html

Caddyfile例子

# 全局选项块, 必须放在头部
{
	# 开启debug模式
	debug
	# 管理端点禁用
	admin   off
	# 日志配置
	log [name] {
		output  <writer_module> ...
		format  <encoder_module> ...
		level   <level>
		include <namespaces...>
		exclude <namespaces...>
	}
	# 自动HTTPS
	auto_https off|disable_redirects|ignore_loaded_certs
	email <yours>
	default_sni <name>
	local_certs
	skip_install_trust
	acme_ca <directory_url>
	acme_ca_root <pem_file>
	acme_eab <key_id> <mac_key>
	acme_dns <provider> ...
	on_demand_tls {
		ask      <endpoint>
		interval <duration>
		burst    <n>
	}
	key_type ed25519|p256|p384|rsa2048|rsa4096
	cert_issuer <name> ...
	ocsp_stapling off
	preferred_chains [smallest] {
		root_common_name <common_names...>
		any_common_name  <common_names...>
	}
}

# 片段, 可以使用import在任何地方引用
(backend) {

   # 反向代理
   reverse_proxy <http://localhost:54321> {
      # header_up为在代理之前+-请求头
      header_up Host {host}
      header_up X-Real-IP {remote}
      header_up X-Forwarded-For {remote}
      header_up X-Forwarded-Port {server_port}
      header_up X-Forwarded-Proto {scheme}
	  # header_down为在代理请求返回之后+-响应头
	  header_down Access-Control-Allow-Headers * 
      header_down Access-Control-Allow-Origin *
   }

}

# 监听9003端口
:9003 {
  # handle 表示拦截器
  handle /prod-api/* {
	import backend
  }

  # handle_path会把拦截到的路径丢弃掉
  handle_path /sc-admin* {
    # root表示文件服务根目录
	root * "D:/soft/phpstudy_pro/WWW/www.sc-one.com/sc-admin/"
	# 跟nginx try_files差不多
	try_files {path} /index.html
	# 指明为文件服务
	file_server
  }
  
  handle_path /sc-h5* {
	root * "D:/soft/phpstudy_pro/WWW/www.sc-one.com/sc-h5/"
	try_files {path} /index.html
	file_server
  }

}