Caddy 1 迁移到 Caddy 2 配置文件改动 

之前一直用的是CADDY 1,最近随着CADDY 2 越来越接近生产环境,故而借由查看服务器情况下载升级了一下。

升级BIN 很简单,直接下载官方提供的BIN 包解压即可。如果要自己编译得话,GOLANG 的版本需要Go 1.14 以上了。 v1的话 1.13 版本就行。

这里不赘述。主要讲讲配置文件格式的变化。改动还是蛮大的,v1的CaddyFile 直接无法启动。需要自己根据官方迁移文档挨个修改。。。。。

这里是我V1 和 V2 的区别.

v1 版本:

sgp.xzx.im
root /home/admin/http
proxy /caonima 127.0.0.1:9700 {
websocket
header_upstream -Origin
}
browse /
status 403 /forbidden
basicauth "username" password{
realm "password plz"
/pdf
/rinima
}
rewrite {
# if {file} starts_with .
r ^/\..*
to /forbidden
}
fastcgi / /run/php-fpm/www.sock php


v2 版本:

sgp.xzx.im {
root * /home/admin/http
reverse_proxy /caonima 127.0.0.1:9700
respond /forbidden 403
basicauth /pdf/* {
username JDJhJDEwJEhrMGVjT2s1ZWNoSnM1VUFhUThnV090dUttU3ZYc1kyZGVTLmhoNGVVZUVtY0lwcXRuRG1T
}
basicauth /rinima/* {
username JDJhJDEwJEhrMGVjT2s1ZWNoSnM1VUFhUThnV090dUttU3ZYc1kyZGVTLmhoNGVVZUVtY0lwcXRuRG1T
}
@dotFiles {
path_regexp ^/\..*
}
rewrite @dotFiles /forbidden
# Proxy PHP files to the FastCGI responder
@phpFiles {
path *.php
}
reverse_proxy @phpFiles unix//var/run/php-fpm-www.sock {
transport fastcgi {
split .php
}
}
#php_fastcgi unix//var/run/php-fpm-www.sock #这个应该是有用的,之前忘了加 unix:// 前缀,还以为没作用
file_server /* browse
}


基本一目了然。要注意的是
1.HTTP 认证密码不在存放明文,跟NGINX、APACHE HTTPD 学了,通过密码工具生成密文。具体看

root@iZt4nbvac3vpa6uqd0l17kZ:~ # caddy help hash-password
Convenient way to hash a plaintext password. The resulting
hash is written to stdout as a base64 string.

--algorithm may be bcrypt or scrypt. If script, the default
parameters are used.

Use the --salt flag for algorithms which require a salt to
be provided (scrypt).

usage:
caddy hash-password --plaintext <password> [--salt <string>] [--algorithm <name>]

flags:
-algorithm string
Name of the hash algorithm (default "bcrypt")
-plaintext string
The plaintext password
-salt string
The password salt

Full documentation is available at:
https://caddyserver.com/docs/command-line
root@iZt4nbvac3vpa6uqd0l17kZ:~ # caddy hash-password --plaintext "caonima"
JDJhJDEwJEV1VTFDbk94WnJFaEZJZndMb0tob081U01JOEtVTEpuMW1tbGZRNW16QXJFb3gubm8yM2RX #生成的密文


2.其次就是 php_fastcgi 其实 reverse_proxy 包装了一下,算是个“存储过程”吧。。。。
负责的事情比v1 版本的 fastcgi 多了很多,因为是专门为PHP 解释器转发设计的。
更方便你部署PHP站点了,特别是lumen 这类bootstrap 单一入口,控制器做转发的框架。
可以省去 try_files 等很多逻辑。一键集成PHP....

具体看:https://caddyserver.com/docs/caddyfile/directives/php_fastcgi#expanded-form


除了配置之外,启动方式也变了。
之前:

echo "start caddy"
nohup go/bin/caddy -agree -log log/web.log -conf cfg/Caddyfile >& log/caddy.log &


现在:

\--- 17397 root caddy run --pingback 127.0.0.1:26860 --config cfg/Caddyfile
# start Starts the Caddy process in the background and then returns
默认后台运行,不需要再用 nohup 了


注:本文在实践&撰写时参考了以下文档:
https://caddyserver.com/docs/v2-upgrade
https://caddyserver.com/docs/caddyfile/directives
https://caddyserver.com/docs/caddyfile/matchers
https://caddyserver.com/docs/caddyfile-tutorial

评论