Caddy V2 使用记录 作者: Hogwarts 发布于: 2023-07-30 更新于: 2026-02-19 分类: 默认分类 默认的CaddyV2是不带Webdav模块的,使用起来太不方便了,得先弄个带Webdav模块的Caddy。 #一、编译CaddyV2 ##1.1 Golang环境的安装 参考:`https://winamp.eu.org/91.html` 官网:`https://go.dev/dl/` ##1.2 xcaddy 下载安装 下载地址:`https://github.com/caddyserver/xcaddy/releases` wget https://github.com/caddyserver/xcaddy/releases/download/v0.3.5/xcaddy_0.3.5_linux_amd64.tar.gz tar -zxvf xcaddy_0.3.5_linux_amd64.tar.gz mv xcaddy /usr/local/bin/ xcaddy version ##1.3 源码编译 加了两个插件,可根据个人需求按例子添加。 需指定编译的caddy的版本号。 ###1.3.1 下载源码 git clone https://github.com/caddyserver/caddy.git cd caddy ###1.3.2 编译 xcaddy build v2.7.4 --output ./caddy_amd64_v2.7.4 \ --with github.com/kirsch33/realip \ --with github.com/mholt/caddy-webdav ###1.3.3 交叉编译 GOOS=linux GOARCH=arrch64 xcaddy build v2.7.4 --output ./caddy_arrch64_v2.7.4 \ --with github.com/kirsch33/realip \ --with github.com/mholt/caddy-webdav 其它系统环境可修改 GOOS 和 GOARCH 编译。 ##1.4 查看命令 mv caddy_amd64_v2.7.4 /usr/local/bin/caddy caddy version #版本查看 caddy list-modules #插件查看 #不建议自行编译,建议下载teddysun的LCMP,或下载lxhao61大佬编译的Caddy2。下载地址: `https://dl.winamp.eu.org/caddy/v2/` `https://github.com/lxhao61/integrated-examples/releases` #二、caddy 使用 caddy的官方文档看不大懂,不过对于熟悉 json 写法的同学很顺手,目前我还是侧重于 caddyfile 用法,但这并不是官方所推荐的。记录几个正常已运行的写法,日后再慢慢测试补充。 ##编辑 Caddyfile vim /etc/caddy/Caddyfile { admin off order webdav last #无webdav的省略此项 } :80 { # Set this path to your site's directory. root * /data/www/default encode gzip # Enable the static file server. file_server { index index.html } # Serve a PHP site through php-fpm: php_fastcgi unix//run/php-fpm/www.sock { split .php index index.php } log { output file /var/log/caddy/ssl_access.log { roll_size 100mb roll_keep 3 roll_keep_for 7d } format json { time_format wall } } } import /etc/caddy/conf.d/*.conf 正常运行后在 `/etc/caddy/conf.d/*.conf` 下放入以下单独的 `conf` 即可。 可能还需建立文件目录 mkdir -p /data/www/default mkdir -p /var/log/caddy/ mkdir -p /etc/caddy/conf.d/ useradd -r -d /data/www -M -s /sbin/nologin caddy chown -R caddy.caddy /data/www/default chown -R caddy.caddy /var/log/caddy/ 以上配置抄自:[LCMP (Linux + Caddy + MariaDB + PHP)](https://teddysun.com/700.html "LCMP (Linux + Caddy + MariaDB + PHP)") ##补充说明 - caddy2默认http自动转https,所以不用做80跳转443,并且域名监听时不加端口号。 - 可在两个域名中间以“逗号 + 空格” 或者 “空格” 来分隔 domain.com www.domain.com domain.com, www.domain.com - 同时用两个域名时,Firefox浏览器有时不能很好的响应,建议第二行为tls形式。 domain.com www.domain.com { tls /etc/ssl/domain/fullchain.cer /etc/ssl/domain/private.key } ##2.1 目录浏览 yourdomain.com { log { output file /var/log/caddy/access.log } root * /data/www/html file_server browse tls /etc/caddy/fullchain.cer /etc/caddy/private.key } ##2.2 webdav yourdomain.com { encode gzip tls /etc/caddy/fullchain.cer /etc/caddy/private.key file_server route { basicauth { user yourpasswd #不支持明文密码。(hashed_password_base64) 密码生成命令:caddy hash-password --plaintext yourpasswd } webdav /* { root /webdavPath prefix / } } } 如果是主域名加路径模式则需加入`prefix`选项 webdav /webdav/* { root /mnt/M/share/ prefix /webdav } 参考:`https://www.mrdoc.fun/doc/478/` `https://github.com/mholt/caddy-webdav` > webdav后面代表匹配的路径,在访问/webdav/xxx这类子路径时生效,root代表文件系统路径,prefix前缀用于修正访问文件目录的路径,比如访问/webdav/img/1.png,如果没有设置prefix,默认就会去文件系统访问/mnt/M/share/webdav/img/1.png,加上prefix参数,系统就会知道访问路径开头的/webdav是需要去掉的。webdav不能像caddy1一样设置只读,所以只能通过配置要访问的文件对运行服务的caddy用户的读写权限实现。 当file_server和webdav同时使用时,需要加上 route,原因不太清楚。 **webdav的设置较为繁琐,需不断调试,设置不当可能无法连接;或basicauth不起作用。因需求不同,故配置不同。** ##2.3 路径反代 yourdomain.com { encode gzip log { output file /var/log/access.log } tls /etc/caddy/pfullchain.cer /etc/caddy/private.key root * /data/www/html/ file_server browse reverse_proxy /path 127.0.0.1:port #可以给xray-plugin(WS为HTTP/1.1模式,不建议。建议使用gRPC模式HTTP/2。) header { Strict-Transport-Security "max-age=31536000; preload" X-Content-Type-Options nosniff X-Frame-Options SAMEORIGIN } } 仅简单记录这几个caddyfile的写法吧,还不能写在一块,写在一个caddyfile中会报错。按caddy官方说法需要像nginx的upstram那样设置route。边学习边补充吧。 ##2.4 前置反向代理 yourdomain.com { encode gzip zstd log { output file /var/log/access.log } tls /etc/caddy/fullchain.cer /etc/caddy/private.key reverse_proxy https:/yourdomain.com { transport http { tls tls_insecure_skip_verify } header_up Host yourdomain.com header_up X-Forwarded-Host yourdomain.com } } ##2.5 PHP写法 yourdomain.com { encode gzip zstd log { output file /var/log/access.log } tls /etc/caddy/fullchain.cer /etc/caddy/private.key root * /data/www/html/ file_server browse php_fastcgi unix//run/php/php8.2-fpm.sock { split .php index index.php } header { Strict-Transport-Security "max-age=31536000; preload" X-Content-Type-Options nosniff X-Frame-Options SAMEORIGIN } } ##2.6 本地端口反代 yourdomain.com { log { output file /var/log/access.log } encode gzip file_server tls /etc/caddy/fullchain.cer /etc/caddy/private.key reverse_proxy 127.0.0.1:port { header_up Host {upstream_hostport} } } ##2.7 日志 log { output file /var/log/caddy/ssl_access.log { roll_size 100mb roll_keep 3 roll_keep_for 7d } format json { # 将时间戳格式化为可读的本地时间 time_format wall } } 注意时区: Caddy 的 wall 格式默认读取的是服务器操作系统的时区。 #设置为上海(北京)时区 timedatectl set-timezone Asia/Shanghai systemctl stop caddy systemctl start caddy systemctl status caddy ##2.8 网站主路径下挂另一个静态网站 本例由LCMP和Gemini提供 yourdomain.com { tls /etc/ssl/fullchain.cer /etc/ssl/private.key # 1. 安全过滤:放在最前面 @badbots { header_regexp User-Agent "(?i)censys|shodan|zoomeye|ahrefs|mj12|semrush|dotbot|libwww-perl|nmap|masscan|dirbuster|sqlmap|nikto|wpscan|whatweb|wget|fetch|httpclient|crawler|scrapy|httpx|netcraft|zgrab|nessus|openvas" } handle @badbots { respond "Access for bad crawlers denied" 403 { close } } # 全局头部和日志 header { Strict-Transport-Security "max-age=31536000; preload" X-Content-Type-Options nosniff X-Frame-Options SAMEORIGIN } encode gzip log { output file /var/log/caddy/ssl_access.log { roll_size 100mb roll_keep 3 roll_keep_for 7d } format json { time_format wall } } # 2. 特定路径处理 handle /path1* { @noSlash { path /path1 } redir @noSlash /path1/ root * /data/www/path1 php_fastcgi unix//run/php/php-fpm.sock uri strip_prefix /path1 file_server } handle /path2* { @NoSlash { path /path2 } redir @NoSlash /path2/ root * /data/www/path2 uri strip_prefix /path2 file_server } handle /path3* { @ituNoSlash { path /path3 } redir @ituNoSlash /path3/ root * /data/www/bi3aax/path3 uri strip_prefix /path3 file_server } # 3. 默认主站处理 (排除掉上面已处理的路径) handle { root * /data/www/html php_fastcgi unix//run/php/php-fpm.sock file_server } } ##2.9 gRPC 模式 由Gemini提供 # 1. 定义 gRPC 特征匹配器 (重点:不再依赖路径) @grpc { header Content-Type application/grpc* } # 2. 只要是 gRPC 流量,无论路径是 //Tun 还是 /SpeedTest,都转发到 10443 reverse_proxy @grpc h2c://127.0.0.1:10443 { transport http { versions h2c 2 } } # 3. 处理普通 Web 业务反代 (18000 端口) reverse_proxy 127.0.0.1:18000 { header_up Host {upstream_hostport} } v2ray-plugin设置 插件程序: v2ray-plugin 插件选项: mode=grpc;tls;host=yourdomain.com ##2.10 基础防护(拦截敏感目录和文件) Yourdomain.com { # 1. 定义一个拦截匹配器 @forbidden_paths { # 拦截尝试直接访问 wp-includes 下的 PHP 文件 path /wp-includes/*.php # 拦截尝试访问 wp-content 下的 PHP 文件(通常是后门或漏洞点) path /wp-content/*.php # 拦截常见的扫描路径 path /xmlrpc.php path /.env path /.git* path /wp-includes/wp-class.php } # 2. 对匹配到的请求直接拒绝 respond @forbidden_paths "Access Denied" 403 # 正常 WordPress 反代或配置 php_fastcgi unix//run/php/php-fpm.sock file_server } #三、开机启动和可能的错误 ##3.1 开机启动 vim /etc/systemd/system/caddy.service [Unit] Description=Caddy After=network-online.target [Service] User=caddy Type=simple CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE NoNewPrivileges=true ExecStart=/usr/local/bin/caddy run --config /etc/caddy/Caddyfile --adapter caddyfile Restart=on-abort [Install] WantedBy=default.target systemctl start caddy systemctl enable caddy systemctl status caddy --no-page -l ##3.2 运行当中的报错 ###3.2.1 报错一 Log报错:Caddyfile input is not formatted 运行:`caddy fmt --overwrite` 参考:`https://github.com/NixOS/nixpkgs/issues/216149` ###3.2.2 报错二 按教程` https://teddysun.com/700.html `像nginx那样弄一个 conf.d 将上述caddyfile改为conf文件放入后报错:`Error: adapting config using caddyfile: server block without any key is global configuration, and if used, it must be first` 解决:将order加入到caddyfile文件中。由于该指令不是 Caddy 的标准配置,因此您需要对该指令进行排序。 { admin off order webdav last #无webdav的省略此项 } ###3.2.3 报错三 `Error: loading initial config: loading new config: http app module: start: listening on:80: listen tcp:80: bind: permission denied` 运行:`setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy` #四、参考 [Caddy 2安装与配置](https://qoant.com/2021/02/vps-caddy2/ "Caddy 2安装与配置") [Caddy 2 简明教程](https://jivps.com/21.html "Caddy 2 简明教程") 标签: caddy, caddy2, caddyv2