Caddy2替换nginx以及使用
安装篇(centos7)
sudo yum install yum-plugin-copr -y
sudo yum copr enable @caddy/caddy -y
sudo yum install caddy -y
caddy是因为插件得扩展,扩展插件好用得原因不像nginx重新下载源码编译那么麻烦
直接访问https://caddyserver.com/download 然后鼠标可以选中要用得插件直接下载二进制替换即可
cp $(which caddy) $(which caddy)_$(caddy_v2.5.0 version |awk '{print$1}')
mv caddy_v2.5.0-rc.1 $(which caddy)
caddy常用命令
# 检查配置
caddy validate
# 重载配置
caddy reload
# 停止
caddy stop
# 查看模块
caddy list-modules
# 生成一条命令
caddy adapt --config caddy.yaml --adapter yaml
# 验证 Caddyfile 有效性
caddy adapt --config ./Caddyfile --validate
# 格式化配置
caddy fmt
caddy fmt -overwrite Caddyfile
# 启动测试
caddy run -config /etc/caddy/Caddyfile
caddy配置(列举一些常用得demo) 配置路径 /etc/caddy/Caddyfile
1.做个获取公网ip得api
http://ip.1nth.com:80 https://ip.1nth.com {
respond / 200 {
body "{remote_host}
"
}
}
2.使用阿里云ack自动获取证书
*.1nth.com {
tls {
dns alidns {
access_key_id "access_key_id"
access_key_secret "access_key_secret"
}
}
import LOG "/var/log/caddy/caddy.log"
}
3.静态文件
:80 {
root * /var/www/html
file_server
}
4.目录列表服务(说白了就是文件服务器)
:88 {
root * /opt
file_server browse
}
:88 {
route /files/* {
#redir /myki /
uri strip_prefix /files
root * /opt
file_server browse
}
}
5.同时映射多个地址
localhost:8888,
psvmc.cn,
www.psvmc.cn {
root * /data/myblog
tls /etc/caddy/cert/psvmc.pem /etc/caddy/cert/psvmc.key
file_server
}
6.重定向(http跳转https)
www.1nth.com {
redir https://1nth.com{uri} permanent
}
7.负载均衡 反向代理
www.1nth.com {
reverse_proxy localhost:9000 localhost:9001 {
lb_policy first
}
}
8.Websocket反向代理
ws.1nth.com {
## HTTP 代理配置
## 此时访问 ws.1nth.com,实际访问的是 127.0.0.1:8080/app/ 的内容
reverse_proxy / 127.0.0.1:8080/app/
## WebSocket 代理配置
## 客户端请求的 wss://ws.1nth.com/ws, 实际为 wss://127.0.0.1:8080/ws
reverse_proxy /ws 127.0.0.1:8080
}
9.跨域访问
:2020 {
@options {
method OPTIONS
}
header {
Access-Control-Allow-Origin "{http.request.header.Origin}"
Access-Control-Allow-Credentials true
Access-Control-Allow-Methods *
Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization
}
reverse_proxy 127.0.0.1:8080
respond @options 204
}
自己编译需要得模块caddy (Ubuntu 22.04)
# 安装go
sudo tar -zxvf go1.18.1.linux-amd64.tar.gz -C /usr/local/
# 配置go环境变量
mkdir -p ${HOME}/.go/{src,bin,pkg}
vi /etc/.bashrc
export GO111MODULE=on
export GOPROXY='https://goproxy.cn,direct'
export GOROOT=/usr/local/go
export GOPATH="${HOME}/.go"
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
go version
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
# 安装 xcaddy
go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
# 安装完成后应当在命令行可以直接执行 xcaddy 命令
xcaddy version
# 编译之前系统需要安装 jq、curl、git
apt install -y curl git jq
# 获取最新版本号,其实直接去 GitHub realse 页复制以下就行
# 这里转化为脚本是为了方便自动化
#获取caddy2的最新版本号,并导入变量version
export version=$(curl -s "https://api.github.com/repos/caddyserver/caddy/releases/latest" | jq -r .tag_name)
#2.4.6编译报错 用最新版编译
export version=v2.5.0
# 使用 xcaddy 编译 https://jivps.com/21.html https://mritd.com/2021/01/07/lets-start-using-caddy2/
# xcaddy build ${version} --output ./caddy_${version}
xcaddy build ${version} --output ./caddy_${version} \
--with github.com/abiosoft/caddy-exec \
--with github.com/caddy-dns/cloudflare \
--with github.com/caddy-dns/dnspod \
--with github.com/caddy-dns/namecheap \
--with github.com/caddy-dns/alidns \
--with github.com/caddy-dns/duckdns \
--with github.com/caddy-dns/gandi \
--with github.com/caddy-dns/route53 \
--with github.com/greenpau/caddy-security \
--with github.com/greenpau/caddy-trace \
--with github.com/hairyhenderson/caddy-teapot-module \
--with github.com/kirsch33/realip \
--with github.com/porech/caddy-maxmind-geolocation \
--with github.com/caddyserver/transform-encoder \
--with github.com/mholt/caddy-webdav \
--with github.com/mholt/caddy-l4 \
--with github.com/ggicci/caddy-jwt
文章作者 🐳Myki
上次更新 2022-05-01