由于国内ip无法访问OpenAI的API,因此对API反代在某些情况下是有用的。使用宝塔面板反代OpenAI API的具体方法如下:
首先在你支持访问OpenAI API的服务器的宝塔面板上创建一个网站,静态网站即可,记得配置SSL;
然后在设置里添加反向代理,目标URL:https://api.openai.com,其他保持默认,提交;
点击配置文件,粘贴以下内容:
#PROXY-START/
location ^~ /
{
proxy_pass https://api.openai.com;
proxy_set_header Host api.openai.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
proxy_set_header Authorization “Bearer sk-xxx”;
# proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
proxy_set_header Accept-Encoding “”;
set $static_fileMMEjlOMy 0;
if ( $uri ~* “.(gif|png|jpg|css|js|woff|woff2)$” )
{
set $static_fileMMEjlOMy 1;
expires 1m;
}
if ( $static_fileMMEjlOMy = 0 )
{
add_header Cache-Control no-cache;
}
}
#PROXY-END/
将sk-xxx换成自己的OpenAI API Key即可,如果不想带key请求,删除那一行即可,带key的话自用比较方便。