网站搭建
技术教程
ZAP-Hosting Gameserver and Webhosting

Caddy,自动HTTPS、反向代理建站

Caddy 是一款开源 Web 服务器,用 Go 编写的自动 HTTPS。

1、创建Caddyfile和index.html

mkdir -p /opt/web/{caddy,html}
touch /opt/web/caddy/Caddyfile
touch /opt/web/html/index.html

2、配置index.html

下载新的index.html,覆盖旧的/opt/web/html/index.html

3、配置Caddyfile

配置IP访问的静态页面

http:// {
    root * /usr/share/caddy
    encode gzip
    file_server
}

配置带域名的静态页面

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

配置带域名的反向代理

域名.com {
    reverse_proxy 127.0.0.1:8080
    encode gzip
}

配置带域名的重定向

域名.com {
    redir https://baidu.com{uri}
}

配置带域名的php动态站点

域名.com {
    root * /usr/share/caddy/wordpress
    encode gzip
    php_fastcgi php:9000
    file_server
}

4、docker部署caddy

docker run -d \
  --name caddy \
  --network host \
  --restart unless-stopped \
  -v /opt/web/caddy/:/etc/caddy/ \
  -v /opt/web/html/:/usr/share/caddy/ \
  caddy:latest

或者使用 Docker Compose,创建 docker-compose.yml 文件,复制以下内容到文件,保存

services:
  caddy:
    image: caddy:latest
    container_name: caddy
    network_mode: host
    volumes:
      - /opt/web/caddy/:/etc/caddy/
      - /opt/web/html/:/usr/share/caddy/
    restart: unless-stopped
在包含 docker-compose.yml 文件的目录下,执行启动命令:docker compose up -d

重启caddy

docker restart caddy

例如多站点 自动HTTPS、反向代理建站

umami.qqqq.cc {
    reverse_proxy 168.61.66.66:20001
    encode gzip
}

portainer.qqqq.cc {
    reverse_proxy 168.61.66.66:9000
    encode gzip
}

grafana.qqqq.cc {
    reverse_proxy 168.61.66.66:3000
    encode gzip
}
赞(0) 打赏
未经允许不得转载:科技宝典 » Caddy,自动HTTPS、反向代理建站