下载二进制

参加官方文档 
https://caddyserver.com/download

选择Linux service方式手动安装

参加官方文档 
https://caddyserver.com/docs/install#static-binaries
创建 /etc/caddy/Caddyfile
localhost
respond "Hello, world!"
创建 /etc/systemd/system/caddy.service
# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
User=root
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=100000
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target
sudo mv caddy /usr/bin/
caddy version
sudo systemctl daemon-reload
sudo systemctl enable caddy
sudo systemctl start caddy
systemctl status caddy

注:这里使用root用户去运行,官方使用caddy用户会出现一些错误

测试一下

修改 /etc/caddy/Caddyfile 为
localhost {
	respond "Hello, world!"
}

localhost:2016 {
	respond "Goodbye, world!"
}

sudo systemctl restart caddy

测试一下
[root@master ~]# curl https://localhost
Hello, world!
[root@master ~]# curl https://localhost:2016
Goodbye, world!
[root@master ~]# 

静态文件服务器

将服务器目录映射为静态文件服务器

https://caddyserver.com/docs/quick-starts/static-files

tt.xxx.com {
    root * /root/files
    file_server browse {
       hide 1.txt
    }
    tls xxxxxx@gmail.com
}

hide为隐藏不在服务web中显示的文件
browse表示自动生成文件列表

反向代理

webtest.xxx.com {
    reverse_proxy / 127.0.0.1:9090
    tls xxx@gmail.com
}

负载均衡和健康检查

webtest.xxx.com {
    reverse_proxy / 127.0.0.1:9090 127.0.0.1:9091 {
       lb_policy round_robin
       header_up Host {http.reverse_proxy.upstream.hostport}
       health_path /
       health_interval 5s
       health_timeout 5s
    }

    tls xxx@gmail.com
}

round_robin 表示轮询
health_path 表示检查服务健康

使用手册

https://caddyserver.com/docs/getting-started