文章

caddy安装与使用

介绍

Caddy 是一个强大的、易于使用的 web 服务器,以其自动 HTTPS 功能而闻名。它使用 Go 语言编写

Debian、Ubuntu安装caddy

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
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 apt update
sudo apt install caddy

查看版本

caddy version

运行caddy

systemctl start caddy
systemctl enable caddy

caddy默认配置文件在/etc/caddy/caddyfile

配置IP访问的静态页面

http://ip {
    root * /usr/share/caddy
    encode gzip    # gzip压缩
    file_server
}

配置域名访问的静态页面

example.com {
    root * /usr/share/caddy
    encode gzip
    file_server
}

配置反向代理

example.com {
    reverse_proxy 127.0.0.1:8080
}

重定向

example.com {
    redir https://a.example.com{uri}
}

自动https

example.com {
    root * /usr/share/caddy
    tls email@example.com
    file_server
}

使用自定义证书

example.com {
    root * /usr/share/caddy
    file_server
    tls /path/to/cert.pem /path/to/key.pem
}

内网使用自签名证书

example.com {
    root * /usr/share/caddy
    tls internal
    file_server
}

该证书会提示不安全

查看日志:sudo journalctl -u caddy

docker-compose安装caddy

先创建一个空的Caddyfile文件
touch Caddyfile
docker-compose.yml安装

version: "3.7"
services:
  caddy:
    image: caddy
    container_name: caddy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./website:/usr/share/caddy
      - caddy_data:/data
      - caddy_config:/config
    restart: unless-stopped

volumes:
  caddy_data:
  caddy_config:

License:  CC BY 4.0