前言
不管是我们使用桌面客户端,还是网站程序系统,为了国内用户访问GPT更快速,一般都会把前端部署在国内服务器上,同时要和openai的api通信,这里就需要用到api反向代理了。
条件
- 有一台海外VPS(推荐美国,带宽稍微大一点的);
- 域名申请SSL证书。
反代设置步骤
我这里就以我自己的的森鹿数据服务器(在美国的)安装了宝塔面板。
- 第一步:新建站点——绑定域名——纯静态;
- 第二步:设置SSL证书;
- 第三步:复制以下代码添加进网站配置文件。
# ChatGPT-API反向代理规则,注释后配置的反向代理将无效
location / {
if ($request_method = 'OPTIONS') {
return 204;
}
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Headers "Authorization,Cache-Control,Content-Type";
proxy_pass https://api.openai.com/;
proxy_set_header Host api.openai.com;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Accept-Encoding "";
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_ssl_session_reuse off;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
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 X-Forwarded-Proto $scheme;
proxy_max_temp_file_size 0;
resolver 8.8.8.8 ipv6=off;
resolver_timeout 10s;
}完整参考代码:
server {
listen 443 ssl;
server_name {your_domain_name};
ssl_certificate {your_cert_path};
ssl_certificate_key {your_cert_key_path};
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1440m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
location / {
proxy_ssl_server_name on;
proxy_pass https://api.openai.com/;
proxy_set_header Host api.openai.com;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_buffering off;
proxy_cache off;
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 X-Forwarded-Proto $scheme;
chunked_transfer_encoding off;
}
}参考教程
教程1:https://my.oschina.net/u/554046/blog/8703953
教程2:https://blog.csdn.net/a871923942/article/details/129790498
教程3:https://www.iyhua.com/248.html
我的微信
微信扫一扫

评论