docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
version: "3.7"

services:
  caddy:
    image: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /data/caddy/Caddyfile:/etc/caddy/Caddyfile
      - /data/caddy/site:/srv
      - caddy_data:/data
      - caddy_config:/config
    container_name: caddy

volumes:
  caddy_data:
  caddy_config:
1
docker-compose up -d

注意一点 /data/caddy/Caddyfile 需要提前创建好

Caddyfile

Caddy有两种配置格式:1. json 2. Caddyfile,由于Caddyfile较为简洁,因此选用了这种方式。

Example

localhost

respond "Hello, world!"

静态内容

your_domain {
    root * /var/www
    file_server
}

反向代理

your_domain {
    reverse_proxy your_server_ip:port
}

Compare

以下是Json格式和Caddyfile格式的区别:

JSONCaddyfile
Full range of Caddy functionalityMost common parts of Caddy functionality
Easy to generateEasy to craft by hand
Easily programmableDifficult to automate
Extremely expressiveModerately expressive
Allows config traversalCannot traverse within Caddyfile
Partial config changesWhole config changes only
Can be exportedCannot be exported
Compatible with all API endpointsCompatible with some API endpoints
Documentation generated automaticallyDocumentation is hand-written
UbiquitousNiche
More efficientMore computational
Kind of boringKind of fun
Learn more: JSON structureLearn more: Caddyfile docs