🐯

3. 为Open WebUI 开启 HTTPS 加密

💡
这里选择Caddy做HTTPS加密
Why HTTPS Matters 🛡️ 官方说明
HTTPS (Hypertext Transfer Protocol Secure) encrypts communication between your web browser and the Open WebUI server. This encryption provides several key benefits:
  • Privacy and Security: Protects sensitive data like usernames, passwords, and chat content from eavesdropping and interception, especially on public networks.
  • Integrity: Ensures that data transmitted between the browser and server is not tampered with during transit.
  • Feature Compatibility: Crucially, modern browsers block access to certain "secure context" features, such as microphone access for Voice Calls, unless the website is served over HTTPS.
  • Trust and User Confidence: HTTPS is indicated by a padlock icon in the browser address bar, building user trust and confidence in your Open WebUI deployment.
When is HTTPS Especially Important?
  • Internet-Facing Deployments: If your Open WebUI instance is accessible from the public internet, HTTPS is strongly recommended to protect against security risks.
  • Voice Call Feature: If you plan to use the Voice Call feature in Open WebUI, HTTPS is mandatory.
  • Sensitive Data Handling: If you are concerned about the privacy of user data, enabling HTTPS is a crucial security measure.
Choosing the Right HTTPS Solution 官方说明
The best HTTPS solution depends on your existing infrastructure and technical expertise. Here are some common and effective options:
  • Cloud Providers (e.g., AWS, Google Cloud, Azure):
    • Load Balancers: Cloud providers typically offer managed load balancers (like AWS Elastic Load Balancer) that can handle HTTPS termination (encryption/decryption) for you. This is often the most straightforward and scalable approach in cloud environments.
  • Docker Container Environments:
    • Reverse Proxies (Nginx, Traefik, Caddy): Popular reverse proxies like Nginx, Traefik, and Caddy are excellent choices for managing HTTPS in Dockerized deployments. They can automatically obtain and renew SSL/TLS certificates (e.g., using Let's Encrypt) and handle HTTPS termination.
      • Nginx: Highly configurable and widely used.
      • Traefik: Designed for modern microservices and container environments, with automatic configuration and Let's Encrypt integration.
      • Caddy: Focuses on ease of use and automatic HTTPS configuration.
  • Cloudflare:
    • Simplified HTTPS: Cloudflare provides a CDN (Content Delivery Network) and security services, including very easy HTTPS setup. It often requires minimal server-side configuration changes and is suitable for a wide range of deployments.
  • Ngrok:
    • Local Development HTTPS: Ngrok is a convenient tool for quickly exposing your local development server over HTTPS. It's particularly useful for testing features that require HTTPS (like Voice Calls) during development and for demos. Not recommended for production deployments.
Key Considerations When Choosing:
  • Complexity: Some solutions (like Cloudflare or Caddy) are simpler to set up than others (like manually configuring Nginx).
  • Automation: Solutions like Traefik and Caddy offer automatic certificate management, which simplifies ongoing maintenance.
  • Scalability and Performance: Consider the performance and scalability needs of your Open WebUI instance when choosing a solution, especially for high-traffic deployments.
  • Cost: Some solutions (like cloud load balancers or Cloudflare's paid plans) may have associated costs. Let's Encrypt and many reverse proxies are free and open-source.
Caddy做HTTPS(完全自动化)原理 这个过程主要依赖一个名为 ACME (Automatic Certificate Management Environment, 自动证书管理环境) 的开放协议,以及像 Let's EncryptZeroSSL 这样支持 ACME 协议的证书颁发机构 (Certificate Authority, CA)
Caddy 加密流程
  1. 你只需告诉 Caddy 你的网站域名是 example.com
  1. Caddy 这个“机器人”会自动连接 Let's Encrypt 的系统。
  1. Let's Encrypt 会对 Caddy 说:“口说无凭,你必须证明你真的拥有 example.com 这个域名”。
  1. Caddy 会自动完成这个证明过程。
  1. 证明通过后,Let's Encrypt 就会颁发一张有效期为90天的证书,Caddy 会自动下载、安装并启用它。
  1. 在证书快到期前(比如第60天),Caddy 会自动重复一遍上述流程,申请一张新的证书并无缝替换掉旧的。
Caddy加密原理
Caddy 的原理 = 内置 ACME 客户端 + 全自动的域名验证 + 全自动的证书管理和续订。
  1. 启动:Caddy 启动时读取配置文件(Caddyfile),看到一个域名。
  1. 协商:通过 ACME 协议联系 Let's Encrypt,请求证书。
  1. 验证:自动完成 HTTP-01, TLS-ALPN-01 或 DNS-01 挑战之一,以证明域名所有权。
💡

1. HTTP-01 挑战 (最常见)

  • 原理:Let's Encrypt 给 Caddy 一个随机的字符串。Caddy 需要在自己的服务器上创建一个特定的临时文件,路径为 http://example.com/.well-known/acme-challenge/<一个随机文件名>,文件内容就是那个随机字符串。
  • 验证:Let's Encrypt 的服务器会从世界各地访问这个 URL。如果能成功访问并读取到正确的内容,就证明了这台服务器确实控制着 example.com 的 HTTP 流量。
  • 要求:服务器必须开放 80 端口给公网访问。Caddy 会自动处理这个临时文件的创建和删除。

2. TLS-ALPN-01 挑战

  • 原理:这是一个更高级的方法,它在 TLS 握手阶段(建立 HTTPS 连接的第一步)就包含了验证信息。
  • 验证:Let's Encrypt 尝试与服务器建立一个特殊的 TLS 连接,并检查其中是否包含正确的验证信息。
  • 要求:服务器必须开放 443 端口。这个方法的好处是不需要使用 80 端口。

3. DNS-01 挑战 (最强大和灵活)

  • 原理:Let's Encrypt 给 Caddy 一个随机字符串,Caddy 需要在域名的 DNS 设置中,添加一条特定名称的 TXT 记录,其值为这个随机字符串。
  • 验证:Let's Encrypt 的服务器会查询全球的 DNS 系统,检查 example.com 是否有这条 TXT 记录。如果能查到,就证明了你拥有对整个域名的最高控制权。
  • 要求:Caddy 需要获得DNS 提供商(如 Cloudflare, GoDaddy, AWS Route 53 等)的 API 密钥,以便能自动添加和删除 TXT 记录。
  • 优点
    • 服务器可以不向公网暴露任何端口
    • 这是获取通配符证书(例如 .example.com)的唯一方法。
  1. 获取:验证成功后,获取由 Let's Encrypt 签发的 TLS 证书。
  1. 启用:自动在内存中加载证书,并开始通过 HTTPS (443端口) 提供加密服务。
  1. 续订:在证书到期前(通常是到期前30天),自动重复 2-5 步,实现无缝续期。

操作配置

  1. 配置域名
  • 自己的域名:例如 xy.stat200.net
  • 配置好 DNS:需要为域名添加一条 A 记录
  • 验证 DNS 配置:在进行下一步之前,在本地电脑ping your-domain.com,并确认它返回的是服务器的正确 IP 地址。
notion image
  1. 配置文件

详细配置步骤

第一步:准备项目文件

进入项目文件夹(例如 C:\open-webui)。确保这个文件夹里有我们之前创建的 litellm-config.yaml 文件。现在,我们将更新 docker-compose.yml 并创建 Caddyfile
项目文件夹最终应该包含以下文件:
  • docker-compose.yml (更新它)
  • litellm-config.yaml (之前已创建)
  • Caddyfile (新建)

第二步:创建并配置 Caddyfile

项目文件夹中,创建一个名为 Caddyfile 的文件(没有后缀名),并粘贴以下内容。
请务必将 your-domain.com 替换为自己的真实域名。
代码段
# /path/to/your/project/Caddyfile # 将 your-domain.com 替换为自己的域名 # Caddy 将会自动为这个域名申请并配置 HTTPS 证书 xy.stat200.net { # 将所有网络请求反向代理到 Open WebUI 服务 # "open-webui" 是 docker-compose.yml 文件中定义的服务名 # "8080" 是 Open WebUI 在容器内部监听的端口 reverse_proxy open-webui:8080 }
  • 说明:这个配置非常简洁。它告诉 Caddy 监听发往您域名的所有请求,并将它们转发到 Docker 网络内部一个名为 open-webui 的服务的 8080 端口上。

第三步:更新 docker-compose.yml 文件

这是最关键的一步。我们将把 Caddy 作为一个新的服务添加到现有的 docker-compose.yml 文件中,并调整 open-webui 的端口配置。
完全覆盖现有的 docker-compose.yml 文件,使用下面这个包含三个服务的最终版本:
YAML
# /path/to/your/project/docker-compose.yml (最终版) services: # Caddy 服务,作为唯一的对外入口和 HTTPS 网关 caddy: image: caddy:latest container_name: caddy restart: unless-stopped ports: # 必须暴露 80 和 443 端口,用于接收公网流量和申请证书 - "80:80" - "443:443" volumes: # 将我们编写的 Caddyfile 挂载到容器中 - ./Caddyfile:/etc/caddy/Caddyfile # 创建数据卷来持久化存储 Caddy 获取到的证书和其他数据 - caddy_data:/data - caddy_config:/config depends_on: - open-webui # Open WebUI 服务,现在只对内服务,不再直接暴露端口 open-webui: image: ghcr.io/open-webui/open-webui:0.1.125 container_name: open-webui # !!! 关键改动:删除了 ports 部分 !!! # 因为所有外部流量都应通过 Caddy 访问,不再需要直接暴露端口 environment: - 'OPENAI_API_BASE_URL=http://litellm:4000' - 'OPENAI_API_KEY=sk-12345' volumes: - open-webui-data:/app/backend/data restart: unless-stopped depends_on: - litellm # LiteLLM 服务,也是一个内部服务 litellm: image: ghcr.io/berriai/litellm:main-stable container_name: litellm_proxy environment: # 统一配置 Azure 连接信息 - AZURE_API_BASE=https://xy-aisecurity.openai.azure.com/ - AZURE_API_KEY=<your key> - AZURE_API_VERSION=2024-02-01 volumes: - ./litellm-config.yaml:/app/config.yaml command: ["--config", "/app/config.yaml", "--host", "0.0.0.0", "--port", "4000"] restart: unless-stopped # 定义所有服务需要用到的具名数据卷 volumes: open-webui-data: caddy_data: caddy_config:
核心改动说明
  • 新增了 caddy 服务:并为其配置了 80443 端口以及所需的数据卷。
  • 移除了 open-webui 服务的 ports 部分:这是为了安全,确保 Open WebUI 不能被绕过 Caddy 直接访问。

第四步:启动并验证

  1. 请确保项目文件夹中包含 docker-compose.yml, Caddyfile, litellm-config.yaml 三个文件。
  1. docker-compose up -d