-
Notifications
You must be signed in to change notification settings - Fork 5
OPENAI API 反向代理教程 使用 caddy
黑墨水鱼 edited this page Apr 1, 2023
·
1 revision
下面是我正在使用的完整 Caddyfile 配置文件内容。具体细节请参考 Caddy 文档。
example.com {
handle /your/secret/api/path {
rewrite /your/secret/api/path /v1/chat/completions
@cors {
method OPTIONS
}
header @cors {
Access-Control-Allow-Origin: "*"
Access-Control-Allow-Methods: "*"
Access-Control-Allow-Headers: "*"
}
@post {
method POST
}
reverse_proxy @post https://api.openai.com {
header_up -*
header_up User-Agent "Deno/1.31.2"
header_up Host {upstream_hostport}
header_up Authorization "Bearer sk-xxx (your TOKEN here)"
header_up Content-Type {http.request.header.content-type}
header_down Access-Control-Allow-Origin "*"
}
}
reverse_proxy * https://a-luckly-website.com {
header_up -*
}
}
此文件配置了三个不同的反向代理。第一个反向代理处理路径 /v2ray,将请求转发到本地地址 127.0.0.1:2333。第二个反向代理处理路径 /your/secret/api/path
,并为此路径设置 CORS 标头,然后将 POST 请求发送到 https://api.openai.com
。最后一个反向代理将所有其他请求转发到 https://a-luckly-website.com
,不做任何其他更改。
此 Caddyfile 允许您在访问后端服务时进行一些有用的更改,如添加 CORS 标头并更改 HTTP 标头。
这个配置文件还有一个很好的特性,是可以让您对外分享您的 API 路径,而不必担心泄露您的 API key。此外,该配置文件还允许您为特定 API 路径指定不同的反向代理规则,减少了被主动探测的风险。总的来说,这个 Caddyfile 可以大大提高您的 API 的可用性和安全性。