简介
-
Caddy 是一个独立的、静态的二进制文件,没有任何外部依赖,因为它使用 Go
语言编写。这些特性构成了项目愿景的重要组成部分,因为它们简化了部署,并减少了生产环境中繁琐的故障排除工作。 -
大多数人将 Caddy 用作 Web 服务器或代理,但从本质上讲,Caddy 是一个服务器的服务器。借助必要的模块,它可以承担任何长时间运行的进程的角色!
-
Caddy 的 API支持动态配置和导出配置。虽然不需要配置文件,但您仍然可以使用它们;大多数用户最喜欢的配置方式是使用 Caddyfile 。配置文档的格式可以通过配置适配器实现多种形式,但 Caddy 的原生配置语言是JSON。
-
Caddy 可编译用于所有主流平台,且无运行时依赖项。
安装
源码编译
git clone "https://github.com/caddyserver/caddy.git"
cd caddy/cmd/caddy/
go build
二进制文件
https://github.com/caddyserver/caddy/releases

debian/ubuntu软件包
稳定版:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg
sudo chmod o+r /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
测试版:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/testing/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-testing-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/testing/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-testing.list
sudo chmod o+r /usr/share/keyrings/caddy-testing-archive-keyring.gpg
sudo chmod o+r /etc/apt/sources.list.d/caddy-testing.list
sudo apt update
sudo apt install caddy
redhat/centos软件包
Fedora:
dnf install dnf5-plugins
dnf copr enable @caddy/caddy
dnf install caddy
CentOS/RHEL:
dnf install dnf-plugins-core
dnf copr enable @caddy/caddy
dnf install caddy
docker镜像
docker pull caddy
快速启动
使用API
首次启动caddy:
caddy start
Caddy 当前处于空闲状态(配置为空)。请为其添加一个简单的配置curl:
curl localhost:2019/load \
-H "Content-Type: application/json" \
-d @- << EOF
{
"apps": {
"http": {
"servers": {
"hello": {
"listen": [":2015"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Hello, world!"
}]
}
]
}
}
}
}
}
EOF
上述命令提交创建较为繁琐,可以将json内容保存到caddy.json文件里提交创建:
cat > caddy.json << 'EOF'
{
"apps": {
"http": {
"servers": {
"hello": {
"listen": [":2015"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Hello, world!"
}]
}
]
}
}
}
}
}
EOF
curl localhost:2019/load \
-H "Content-Type: application/json" \
-d @caddy.json
在浏览器中访问或使用curl访问:
[root@studylinux ~]# curl -w '\n' localhost:2015
Hello, world!

使用Caddyfile
创建一个名为Caddyfile(无扩展名)的新文本文件:
在 Caddyfile 中首先要输入的是你的网站地址:
localhost
然后按回车键,输入你想让它执行的操作,就像这样:
localhost
respond "Hello, world!"
保存此文件,然后从包含 Caddyfile 的同一文件夹运行 Caddy:
caddy start
您可能会被要求输入密码,因为 Caddy 默认使用 HTTPS 协议服务所有网站,包括本地网站。(密码提示只会在第一次访问时出现!)
注:对于本地 HTTPS,Caddy 会自动为您生成证书和唯一的私钥。根证书会被添加到系统的信任存储区,因此需要输入密码。这样您就可以在本地通过 HTTPS 进行开发,而不会出现证书错误。
要么在浏览器中打开localhost,要么curl:
[root@studylinux ~]# curl -w '\n' https://localhost
Hello, world!

您可以在 Caddyfile 文件中用花括号括起来定义多个站点{ }。请将您的 Caddyfile 文件修改为:
localhost {
respond "Hello, world!"
}
localhost:2016 {
respond "Goodbye, world!"
}
您可以通过两种方式向 Caddy 提供更新后的配置,一种是直接使用 API:
curl localhost:2019/load \
-H "Content-Type: text/caddyfile" \
--data-binary @Caddyfile
或者使用 reload 命令,该命令会为您执行相同的 API 请求:
caddy reload
在浏览器中或通过其他方式测试一下你的新“goodbye”端点,curl确保它能正常工作:
[root@studylinux ~]# curl -w '\n' https://localhost:2016
Goodbye, world!

使用完 Caddy 后,请务必将其停止:
[root@studylinux ~]# caddy stop
静态文件
command方式
在终端中,切换到网站的根目录并运行:
[root@studylinux ~]# caddy file-server
如果出现权限错误,则可能意味着您的操作系统不允许您绑定到低端口——因此请改用高端口:
[root@studylinux ~]# caddy file-server --listen :2015
然后,在浏览器中打开localhost(或localhost:2015 )即可查看您的网站!


如果没有索引文件,但又想显示文件列表,请使用以下–browse选项:
[root@studylinux ~]# caddy file-server --browse

您可以使用另一个文件夹作为网站根目录:
[root@studylinux ~]# mkdir mysite
[root@studylinux ~]# echo 'It works!' > mysite/index.html
[root@studylinux ~]# caddy file-server --root ~/mysite

Caddyfile文件方式
在网站根目录下,创建一个名为 .php 的文件,Caddyfile并添加以下内容:
localhost
file_server
如果您没有绑定到低端口的权限,请替换localhost为localhost:2015(或其他高端口)。
localhost:2015
file_server
然后,在同一目录下运行:
[root@studylinux ~]# caddy run
然后您就可以加载localhost(或您配置中的任何地址)来查看您的网站了!
该file_server指令提供了更多自定义网站的选项。更改 Caddyfile 文件后,请务必重新加载Caddy(或停止并重新启动 Caddy)!
如果没有索引文件,但又想显示文件列表,请使用以下browse参数:
localhost
file_server browse
您也可以使用另一个文件夹作为网站根目录:
localhost
root /var/www/mysite
file_server
反向代理
command方式
要在您的计算机上启动从端口 2080 到端口 9000 的纯文本 HTTP 代理:
[root@studylinux ~]# caddy reverse-proxy --from :2080 --to :9000
然后试试看:
[root@studylinux ~]# curl -w '\n' 127.0.0.1:2080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
该reverse-proxy命令旨在实现快速简便的反向代理。(如果您的需求很简单,也可以在生产环境中使用。)
Caddyfile文件方式
在当前工作目录中,创建一个名为 <filename> 的文件,Caddyfile并添加以下内容:
:2080
reverse_proxy :9000
该配置文件大致相当于caddy reverse-proxy上面的命令。
然后,在同一目录下运行:
[root@studylinux ~]# caddy run
然后尝试使用代理:
[root@studylinux ~]# curl -w '\n' 127.0.0.1:2080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
如果更改了 Caddyfile 文件,请务必重新加载Caddy。
reverse_proxy这是一个简单的例子。您可以使用该指令执行更多操作。
客户端到代理的 HTTPS
如果 Caddy 知道主机名(域名),它会自动默认通过 HTTPS 提供代理服务。如果您省略该标志,则caddy reverse-proxy命令将默认执行此操作;或者,您可以将 Caddyfile 的第一行替换为代理的域名。localhost–from
- 如果您使用 localhost 或任何以 .localhost 结尾的域名,Caddy
将使用自动续订的自签名证书。首次使用时,您可能需要输入密码,因为 Caddy 会尝试将其 CA 的根证书安装到您的信任存储区中。 - 如果您使用其他域名,Caddy 将尝试获取公共信任证书;请确保您的 DNS 记录指向您的计算机,并且端口 80 和 443
已向公众开放并指向 Caddy。
如果您不指定端口,Caddy 默认使用 443 端口进行 HTTPS 连接。在这种情况下,您还需要拥有绑定低端口的权限。在 Linux 系统上,有几种方法可以实现这一点:
- 以 root 用户身份运行(例如sudo -E)。
- 或者运行命令sudo setcap cap_net_bind_service=+ep $(which caddy),让 Caddy
具备这种特定功能。
caddy reverse-proxy以下是启用 HTTPS 的最基本命令:
[root@studylinux ~]# caddy reverse-proxy --to :9000
然后试试看:
[root@studylinux ~]# curl -w '\n' https://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
您可以使用该–from标志自定义主机名:
[root@studylinux ~]# caddy reverse-proxy --from caddyserver.localhost --to :9000
如果您没有绑定到低端口的权限,您可以从高端口进行代理:
[root@studylinux ~]# caddy reverse-proxy --from caddyserver.localhost:8443 --to :9000
如果您使用的是 Caddyfile 文件,只需将第一行更改为您的域名,例如:
caddyserver.localhost
reverse_proxy :9000
从代理到后端的 HTTPS
如果后端支持 TLS,Caddy 也可以使用 HTTPS 在自身和后端之间进行代理。只需https://在后端地址中使用:
[root@studylinux ~]# caddy reverse-proxy --from :2080 --to https://caddyserver.localhost
这要求后端证书必须受到 Caddy 运行所在系统的信任。(除非明确配置,否则 Caddy 不会信任自签名证书。)
当然,两端都可以使用HTTPS:
[root@studylinux ~]# caddy reverse-proxy --from webserver.localhost --to https://caddyserver.localhost
这可以实现客户端到代理服务器以及代理服务器到后端服务器之间的 HTTPS 通信。
如果您要代理到的主机名与您要从中代理的主机名不同,则需要使用该–change-host-header标志:
caddy reverse-proxy \
--from webserver.localhost \
--to https://caddyserver.localhost \
--change-host-header
默认情况下,Caddy 会原封不动地传递所有 HTTP 标头,包括 <host>Host和 <host>,并且 Caddy 会从 <host> 标头中获取 TLS ServerName。<host>–change-host-header会将 <host> 标头重置为后端服务器的标头,以便 TLS 握手能够成功完成。在上面的示例中,它会从 webserver.localhost<host> 更改为 <host> caddyserver.localhost(并localhost在 TLS 握手中使用)。
9425

被折叠的 条评论
为什么被折叠?



