暗黑模式
Automatic HTTPS
80/443
Caddy 开箱支持自动化 HTTPS,无需多余的配置。参考:Automatic HTTPS
然而如果机器的 80/443 端口被禁用或占用,可以通过以下方式解决:
在非 80/443 端口监听 HTTP(S) 请求
TIP
- 当机器的 80/443 端口无法使用时,可以自定义 Caddy 的 HTTP/HTTPS 监听端口
- 解决方法要领:
- 全局配置:
{ # 自定义 HTTP 和 HTTPS 端口 http_port 8080 https_port 8443 }
1
2
3
4
5 - Caddy DNS 插件,Why?
- 全局配置:
- 本教程通过自定义 Caddy 的 Docker 镜像来实现此功能,涉及的代码和文档可在此下载:caddy-test-http-port
文件目录结构
.
├── compose.yml # Docker compose
├── conf
│ └── Caddyfile # Caddy config file
├── Dockerfile # Build custom caddy image
└── www # static files
└── index.html
1
2
3
4
5
6
7
2
3
4
5
6
7
Dockerfile
dockerfile
# 使用官方 caddy builder 镜像
FROM caddy:builder AS builder
# 安装 caddy-dns 插件
# 注意: 根据域名提供商选择不同的插件,此处以腾讯的 DNSPOD 为例
# 腾讯域名服务对应的插件:caddy-dns/dnspod
# 阿里域名服务对应的插件:caddy-dns/alidns
RUN xcaddy build \
--with github.com/caddy-dns/dnspod
# 创建最终的运行镜像
FROM caddy:latest
# 从 builder 镜像复制构建的 Caddy 二进制文件
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
compose.yml
yml
services:
caddy:
container_name: caddy-test-http-port
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
- "8443:8443"
- "8443:8443/udp"
environment:
- DNSPOD_TOKEN=ID,TOKEN
volumes:
- ./conf:/etc/caddy
- ./site:/srv
- ./caddy_data:/data
- ./caddy_config:/config
- ./www:/usr/share/caddy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
获取 DNSPOD 的 ID 和 TOKEN
conf/Caddyfile
{
http_port 8080
https_port 8443
}
http://test-http-port.iflyit.top {
redir https://{host}:8443{uri}
}
test-http-port.iflyit.top {
tls {
dns dnspod {env.DNSPOD_TOKEN}
}
# Set this path to your site's directory.
root * /usr/share/caddy
# Enable the static file server.
file_server
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Static files
bash
echo "Hello World!" > www/index.html
1
Start Docker Container
bash
sudo docker compose up
1
Reload caddy
bash
... modify conf/Caddyfile...
docker exec -w /etc/caddy caddy-test-http-port caddy reload
1
2
2