EdgeOne 代理加速 LLM API

Reno 于 2025-07-25 发布

准备工作

添加站点

  1. 点击这里 添加域名,输入站点地址,仅支持一级域,如 example.com,输入后点击下一步
  2. 如果第一步已经填写了邀请码这里会默认选择免费套餐,勾选 我已知悉并同意······ 后点击下一步
  3. 加速区域选择 全球可用区(不含中国大陆)即可,然后选择 NS 或者 CNAME 按要求验证域名,等待域名验证成功后再进入下一步

配置服务

配置DNS

如果上一步选择了NS验证,直接点击 域名管理,左侧菜单选择 DNS记录 添加一条 名为api 的A记录指向服务器IP

如果上一步选择的是CNAME验证,则需要前往域名托管商如Cloudflare处添加

快捷键 Win+R 打开运行,输入 powershell 后回车打开终端,通过ssh连接到服务器

ssh 用户名@服务器IP地址 -p 22

配置防火墙

如果你的服务器开启了防火墙(例如 ufw),需要允许 HTTP 和 HTTPS 流量通过。

# 允许 HTTP (端口 80)
sudo ufw allow 80/tcp

# 允许 HTTPS (端口 443)
sudo ufw allow 443/tcp

# 重新加载防火墙规则
sudo ufw reload

# 查看防火墙状态
sudo ufw status

配置Caddy

首先安装Caddy,下面给出的是Debian系的安装指令,其它系统请参考 官方文档

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
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
chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg
chmod o+r /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

查看服务状态验证安装

systemctl status caddy

修改配置前先备份

cp /etc/caddy/Caddyfile /etc/caddy/Caddyfile.bak

然后再修改 Caddyfile

# 将 api.example.com 替换为你的域名
api.example.com {
    # OpenAI 路由
    handle_path /openai/* {
        reverse_proxy https://api.openai.com {
            header_up Host api.openai.com
        }
    }

    # Anthropic 路由
    handle_path /anthropic/* {
        reverse_proxy https://api.anthropic.com {
            header_up Host api.anthropic.com
        }
    }

    # Gemini 路由
    handle_path /gemini/* {
        reverse_proxy https://generativelanguage.googleapis.com {
            header_up Host generativelanguage.googleapis.com
        }
    }

    # Groq 路由
    handle_path /groq/* {
        reverse_proxy https://api.groq.com {
            header_up Host api.groq.com
        }
    }

    # Cohere 路由
    handle_path /cohere/* {
        reverse_proxy https://api.cohere.ai {
            header_up Host api.cohere.ai
        }
    }

    # XAI 路由
    handle_path /xai/* {
        reverse_proxy https://api.x.ai {
            header_up Host api.x.ai
        }
    }

    # Mistral 路由
    handle_path /mistral/* {
        reverse_proxy https://api.mistral.ai {
            header_up Host api.mistral.ai
        }
    }

    # GitHub 路由
    handle_path /github/* {
        reverse_proxy https://models.inference.ai.azure.com {
            header_up Host models.inference.ai.azure.com
        }
    }

    # 根路径处理
    respond "Welcome to the API Gateway. Only API calls are supported."
}

验证配置

caddy validate --config /etc/caddy/Caddyfile

格式化配置

caddy fmt --overwrite /etc/caddy/Caddyfile

重载配置

sudo systemctl reload caddy

查看日志

 # 实时查看日志
 journalctl -u caddy -f
 
 # 查看所有日志
 journalctl -u caddy --no-pager

加速站点

  1. 首先需要删除先前配置的DNS记录

  2. 然后在 EdgeOne 点击 域名管理 - 添加域名

    • 加速域名 填写 api

    • 源站配置 填写服务器IP地址

点击下一步 ,接着点击 立即启用 并确定,等待部署完成

  1. 点击编辑 HTTPS 配置,勾选 申请免费证书 点击确定,这里检测到国内IP会提示绑卡,如果不需要绑卡可以使用代理绕过

等待部署完成即可

使用代理

这里以 OpenAI 为例

curl -X POST https://api.example.com/openai/v1/chat/completions \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-4.1-mini", "messages": [{"role": "user", "content": "Hello!"}]}'

在客户端中将 https://api.openai.com 替换为 https://api.example.com/openai 即可

参考链接